0

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.

aj_f
  • 932
  • 7
  • 12
vardhanReddy
  • 165
  • 1
  • 7
  • 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 Answers3

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
1

Just give this condition

calendar.onlyShowCurrentMonth=YES;
bummi
  • 27,123
  • 14
  • 62
  • 101
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