OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 Swift 3.0 在 iOS 中禁用以前的日期、月份、年份 JTAppleCalendar [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:49
标题: ios - 使用 Swift 3.0 在 iOS 中禁用以前的日期、月份、年份 JTAppleCalendar

我正在使用这个库:
https://github.com/orazz/CalendarPopUp

假设我有一个用户注册日期,它是 2017 年 9 月 28 日。现在我只想启用下一个日期、月份和年份之间的日期。 应禁用以前的日期、月份和年份。

我该怎么做?

如何禁用滚动和选择上一个日期和月份?

enter image description here



Best Answer-推荐答案


使用它来设置最小和最大日期。您可以更改任何组件,例如日、月、年。

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

关于ios - 使用 Swift 3.0 在 iOS 中禁用以前的日期、月份、年份 JTAppleCalendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46465319/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4