ios - 如何将 UIDatePicker 更改为特定时间(在代码中)

标签 ios swift uidatepicker

我需要将 UIDatePicker 动态更改为特定的。

我的日期选择器仅设置为时间模式。 我可以用

设置时间
timePicker.setDate(NSDate(), animated: false)

但我不知道如何将它更改为不同的时间而不是 当前时间。

那我怎么改呢?

谢谢

最佳答案

您必须更改时间,您可以使用NSDateComponents 并将修改后的日期设置到您的DatePicker

var calendar:NSCalendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit, fromDate: NSDate())
components.hour = 5
components.minute = 50
datePicker.setDate(calendar.dateFromComponents(components)!, animated: true)

关于ios - 如何将 UIDatePicker 更改为特定时间(在代码中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28985483/

相关文章:

php - 如何让 str_replace 识别使用 glob 检索的文件名中的变音符号(在 Mac 上)?

Objective-C ARC __weak 变量引用触发警告 "__weak attribute cannot be specified on an automatic variable"

swift - 带有内部阴影的自定义圆形文本字段未获得正确的布局

datepicker - SwiftUI:DatePicker 是否可以在当前的其他时区显示日期/时间?

iphone - 按日期排序 NSFetchRequest,然后按字母顺序排序

ios - 快速更改日期选择器语言

c# - iOS 版本 Xamarin 应用程序兼容性

ios - 在类别中递归反转 NSString

ios - 无法完成购买非续订订阅交易

ios - 如何消除移至上一屏幕时更改导航栏颜色的延迟?