gpt4 book ai didi

swift - 日期选择器,用户无法返回到之前的日期 Swift

转载 作者:行者123 更新时间:2023-11-30 10:01:49 25 4
gpt4 key购买 nike

我有日期选择器

我已经设置了当前日期,并且我想让用户无法滚动回之前的日期?允许吗?

最佳答案

正如文档所述 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

关于swift - 日期选择器,用户无法返回到之前的日期 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38093342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com