如何在Objective-C中隐藏FSCalendar的月份占位符?
Hey there! I’ve tackled this exact issue with FSCalendar before, so let me share a couple of reliable ways to get rid of those month placeholders:
1. Use the Calendar Delegate Method to Filter Dates
The most straightforward approach is to implement the calendar:shouldDisplayDate: delegate method. This lets you control which dates are rendered on the calendar. Just return NO for any date that isn’t part of the current month:
// Objective-C - (BOOL)calendar:(FSCalendar *)calendar shouldDisplayDate:(NSDate *)date { NSDateComponents *components = [calendar.calendar components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date]; NSDateComponents *currentComponents = [calendar.calendar components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:calendar.currentPage]; return (components.month == currentComponents.month && components.year == currentComponents.year); }
// Swift func calendar(_ calendar: FSCalendar, shouldDisplay date: Date) -> Bool { let components = calendar.calendar.dateComponents([.month, .year], from: date) let currentComponents = calendar.calendar.dateComponents([.month, .year], from: calendar.currentPage) return components.month == currentComponents.month && components.year == currentComponents.year }
This will prevent any dates outside the currently displayed month from showing up entirely.
2. Customize the Appearance of Placeholder Dates
If you don’t want to remove them from the layout but just make them invisible, you can tweak the calendar’s appearance properties to make placeholder dates blend into the background:
// Objective-C calendar.appearance.placeholderColor = [UIColor clearColor]; calendar.appearance.placeholderBackgroundColor = [UIColor clearColor];
// Swift calendar.appearance.placeholderColor = .clear calendar.appearance.placeholderBackgroundColor = .clear
3. Target the Placeholder View Directly (Advanced)
If the above methods don’t fit your use case, you can directly access and hide the internal FSCalendarMonthPlaceholderView instances by traversing the calendar’s subviews:
// Objective-C - (void)hidePlaceholderViews { for (UIView *subview in calendar.subviews) { if ([subview isKindOfClass:NSClassFromString(@"FSCalendarMonthPlaceholderView")]) { subview.hidden = YES; } } }
// Swift func hidePlaceholderViews() { for subview in calendar.subviews { if subview.className == "FSCalendarMonthPlaceholderView" { subview.isHidden = true } } }
Call this method after the calendar has finished loading (like in viewDidAppear: or after reloading the calendar). Note that this relies on internal class names, which might change in future FSCalendar updates, so use it with caution.
内容的提问来源于stack exchange,提问作者Videh Jaiswal

