日期选择器,用户无法返回到上一个日期 Swift

Date Picker, User cannot go back to previous date Swift

我有日期选择器

我已经设置了当前日期,我想让用户无法滚动回到上一个日期?允许吗?

在 Swift 3:

var datePicker = UIDatePicker()
datePicker.minimumDate = Date()

这是如何在 Swift 2.2:

中执行此操作的示例
// a global var
var datePicker = UIDatePicker()

 // UIDatePicker settings:
 let currentDate: NSDate = NSDate()
 let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
 calendar.timeZone = NSTimeZone(name: "UTC")!
 let components: NSDateComponents = NSDateComponents()
 components.calendar = calendar
 components.year = -18
 let minDate: NSDate = calendar.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
 components.year = -150
 let maxDate: NSDate = calendar.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
 datePicker.dateOfBirthUIDatePicker.minimumDate = minDate
 datePicker.dateOfBirthUIDatePicker.maximumDate = maxDate

如文档所述here 您只需设置 UIDatePicker 的选项 minimumDate

这是一个例子

var datePicker = UIDatePicker() //your UIDatePicker
datePicker.minimumDate = Date() //the minimum date is now. Only future date is allowed

另一种情况如下

var datePicker = UIDatePicker() //your UIDatePicker

//create a gregorian calendar
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!

// set the timezone
calendar.timeZone = NSTimeZone(name: "UTC")!

// create and set the component (for your minimum date value)
let components: NSDateComponents = NSDateComponents()
components.calendar = calendar
components.year = -100

// obtain the minimum NSDate, according to the defined components
//it will result as the difference of the years (components.year) from now (NSDate())
let minDate100YearsAgo: NSDate = calendar.dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))!

//then you add this to the UIDatePicker
datePicker.minimumDate = minDate100YearsAgo