如何为timePicker设置限制
How to set restriction for timePicker
这里是通过 UIDatePickerMode.time
使用 timePicker
现在,我的当前时间是 3:13 下午,但是我想在时间选择器中显示从现在起 30 分钟,即 3:43 下午,这意味着用户可以 select 时间3: 43 PM开始。我该如何实现?
这是我试过的代码
我尝试给出 timePicker?.minimumDate
。但是没用
func timePicking(){
let timePicker = ActionSheetDatePicker(title: "Time", datePickerMode: UIDatePickerMode.time, selectedDate: NSDate() as Date!, target: self, action: #selector(self.timeSelected), origin: (sender as AnyObject).superview!?.superview)
let minimumTimeRestrict: TimeInterval = 0 * 30 * 60 * 60
timePicker?.minimumDate = NSDate(timeInterval: -minimumTimeRestrict, since: NSDate() as Date) as Date!
timePicker?.show()
}
Swift 4.0
如果您想在 UIDatePicker
中显示 30 分钟后的时间,请添加 minimumDate
,如下所示。
let nextTime = Calendar.current.date(byAdding: .minute, value: 30, to: Date())
timePicker.minimumDate = nextTime
这里是通过 UIDatePickerMode.time
使用 timePicker现在,我的当前时间是 3:13 下午,但是我想在时间选择器中显示从现在起 30 分钟,即 3:43 下午,这意味着用户可以 select 时间3: 43 PM开始。我该如何实现?
这是我试过的代码
我尝试给出 timePicker?.minimumDate
。但是没用
func timePicking(){
let timePicker = ActionSheetDatePicker(title: "Time", datePickerMode: UIDatePickerMode.time, selectedDate: NSDate() as Date!, target: self, action: #selector(self.timeSelected), origin: (sender as AnyObject).superview!?.superview)
let minimumTimeRestrict: TimeInterval = 0 * 30 * 60 * 60
timePicker?.minimumDate = NSDate(timeInterval: -minimumTimeRestrict, since: NSDate() as Date) as Date!
timePicker?.show()
}
Swift 4.0
如果您想在 UIDatePicker
中显示 30 分钟后的时间,请添加 minimumDate
,如下所示。
let nextTime = Calendar.current.date(byAdding: .minute, value: 30, to: Date())
timePicker.minimumDate = nextTime