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


in code, have uitextfield when user taps on opens uidatepicker enable user , efficiently scroll through date of birth. obviously, wouldn't want uidatepicker scroll 2015 , on does. it's date of birth entry field, need able limit entries 16years+. how do this?

class signupviewcontroller: uiviewcontroller, uitextfielddelegate {      var datepicker:uidatepicker!      @iboutlet weak var datetextfield: uitextfield!      override func viewdidload() {         super.viewdidload()          // ui date picker setup          var customview:uiview = uiview(frame: cgrectmake(0, 100, 320, 160))         customview.backgroundcolor = uicolor.clearcolor()          datepicker = uidatepicker(frame: cgrectmake(0, 0, 320, 160))         datepicker.datepickermode = uidatepickermode.date          customview.addsubview(datepicker)         datetextfield.inputview = customview         var donebutton:uibutton = uibutton (frame: cgrectmake(100, 100, 100, 44))         donebutton.settitle("done", forstate: uicontrolstate.normal)         donebutton.addtarget(self, action: "datepickerselected", forcontrolevents: uicontrolevents.touchupinside)         donebutton.backgroundcolor = uicolor .graycolor()         datetextfield.inputaccessoryview = donebutton 

you can use datebyaddingunit , subtract 16 years current date set maximum date datepicker 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()) 

Comments