简体   繁体   中英

UIDatePicker max and min date range

I develop my own app and I work on sign up screen.The person who sign up my app have to enter a birth date in my textfield and only user has 18-55 age accept.My textField has UIDatePicker.When datePicker open,I want to show min and max date range.For example today now 12/02/2022, datePicker have to show min date 12/02/2004 (user has 18) and show max date 1/1/1967 (user has 55). 在此处输入图像描述

https://imgur.com/a/DBpT5eq

first change the version checking line to

if #available(iOS 13.4,*) {

}

secondly use calendar for min max date

        let datePicker = UIDatePicker()
        if let eighteenAgo = Calendar.current.date(byAdding: .year, value: -18, to: Date()),
           let afterFiftyFive =  Calendar.current.date(byAdding: .year, value: 55, to: Date()) {
            print(eighteenAgo.formatted(), ": Min")
            print(afterFiftyFive.formatted(), ": Max")
            datePicker.minimumDate = eighteenAgo
            datePicker.maximumDate = afterFiftyFive
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM