问题
I am using this library:
https://github.com/orazz/CalendarPopUp
Let's say I have a date for registration of user and it's 28/9/2017. Now I want to only enable dates between next date, month and year. Previous dates, month and year should be disabled.
How can I do that?
How do I disable scrolling and selecting of previous date and month?
回答1:
Use this to set minimum and maximum date. You can change any component like Day, month, year.
Objective c
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setMonth:3];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setMonth:-3];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
Swift
let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let currentDate: NSDate = NSDate()
let components: NSDateComponents = NSDateComponents()
components.month = -3
let minDate: NSDate = gregorian.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
components.month = 3
let maxDate: NSDate = gregorian.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
self.datePicker.minimumDate = minDate
self.datePicker.maximumDate = maxDate
来源:https://stackoverflow.com/questions/46465319/disabling-previous-date-month-year-jtapplecalendar-in-ios-using-swift-3-0