0.代码
@IBOutlet weak var myDatePicker: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
myDatePicker.addTarget(self, action: #selector(chooseDate( _:)), for:UIControlEvents.valueChanged)
}
@objc func chooseDate(_ datePicker:UIDatePicker) {
print("chooseDate")
let chooseDate = datePicker.date
let dateFormater = DateFormatter.init()
dateFormater.dateFormat = "YYYY-MM-dd-HH-mm-ss"
selectedStr = dateFormater.string(from: chooseDate);
print(dateFormater.string(from: chooseDate))
}
1.Preferred Style, preferredDatePickerStyle图示,ios14.2
.automatic .compact
.wheels
.inline
2.纯代码需要iOS13.4以上才能设置preferredDatePickerStyle属性,但是storyboard,iOS10以上,就可以设置
- 虽然可以设置但是ios13.4以下就只有.wheels一种样式(本人在iphone6s, ios12.5.1, 上亲测的结论)
3.用UIControl的下面几个方法,可能可以通过代码调用直接弹出时间选择器,不需要点击UIDatePicker
参考博客:
40-Swift之UIDatePicker(日期选择器) UIDatePicker 日期/时间选取器(滚轮)—IOS开发
|