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

后端 未结 2 1358
傲寒
傲寒 2020-11-28 13:36

In my code, I have a UITextField that when the user taps on opens a UIDatePicker to enable the user to easily and efficiently scroll through to their Date Of Birth. Obviousl

2条回答
  •  一个人的身影
    2020-11-28 14:25

    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 10.2.1 • Swift 5

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

提交回复
热议问题