I am looking for how to disable past dates in the CKCalendar. For instance, I want to disable all dates before the current date. Only today's date and the future date must be clickable.
Asked
Active
Viewed 2,029 times
0
-
Pure guess, seeing only a few code, but in `calendar:willSelectDate`, return false, if the date is from the past. If, and only that's what you're talking about: https://github.com/jaykz52/CKCalendar Same idea with `calendar:willChangeToMonth:`. – Larme Jun 10 '14 at 07:18
3 Answers
4
In addition to Larme's comment (setting calendar:willSelectDate to return NO for past dates), if you want to configure the colors for those "disabled dates", you can do so by setting up the delegate method configureDateItem: forDate:.
An example:
- (void)calendar:(CKCalendarView *)calendar configureDateItem:(CKDateItem *)dateItem forDate:(NSDate *)date {
if([date laterDate:minimumDate] == minimumDate) {
dateItem.textColor = [UIColor grayColor];
}
}
The above code sets the text color of all past dates to gray.
An example for willSelectDate delegate method:
- (BOOL)calendar:(CKCalendarView *)calendar willSelectDate:(NSDate *)date {
if ([date laterDate:minimumDate] == minimumDate) {
return NO;
}
return [calendar dateIsInCurrentMonth:date];
}
The above code disables selection of all past dates.

aj_f
- 932
- 7
- 12
-
is there any way i can add background image instead of color? – Syed Faraz Haider Zaidi Oct 25 '15 at 16:12
-
0
If you are looking SwiftUI solution..
First you need to make partial range from in case you want to make previous dates unelectable
let dateRange: PartialRangeFrom<Date> = {
let calendar = Calendar.current
let startComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: Date.now)
return calendar.date(from: startComponents)!...
}()
then you can configure Date picker passing the range, you just created
DatePicker("", selection: $dueDate, in: dateRange)
.datePickerStyle(.graphical)
.labelsHidden()

Kraming
- 217
- 3
- 8