Limiting UIDatePicker dates from a particular time. Such as Input DOB to a restricted age limit

元气小坏坏 提交于 2019-11-26 22:58:52

You can use dateByAddingUnit and subtract 16 years from current date to set the maximum date for your datePicker as follow:

datePicker.maximumDate = NSCalendar.currentCalendar().dateByAddingUnit(.Year, value: -16, toDate: NSDate(), options: [])

Xcode 8 beta 6 • Swift 3

datePicker.maximumDate = Calendar.current.date(byAdding: .year, value: -16, to: Date())

UIDatePicker has a maximumDate that you can set. Simply set the mode to "Date" in IB and add: datePicker.maximumDate = NSDate(timeIntervalSinceNow: -504911232)

-504911232 means 16 before today(accounting leap years)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!