为了方便操作,在UIAlertAction上直接添加了UIDatePicker控件,这样弹出日期控件直接选择,方便快捷, 在升级了手机系统(iOS14)后,发现样式变了!
这是正常的样子:
@objc func startAction(sender: UITapGestureRecognizer) {
let message = "\n\n\n\n\n\n"
let alert = UIAlertController(title: "开始时间", message: message, preferredStyle: UIAlertController.Style.actionSheet)
alert.isModalInPopover = true
let datePicker = UIDatePicker(frame: CGRect(x: 10, y: 20, width: screenWidth() - 40, height: 160))
datePicker.datePickerMode = .date
datePicker.addTarget(self, action: #selector(startDatePickerValueChanged(datePicker:)), for: .valueChanged)
alert.view.addSubview(datePicker)
let okAction = UIAlertAction(title: "确定", style: .default, handler: {
(alert:UIAlertAction!) -> Void in
//TODO:
}
})
alert.addAction(okAction)
let cancelAction = UIAlertAction(title: "取消", style: .destructive, handler: nil)
alert.addAction(cancelAction)
self.parent?.present(alert, animated: true, completion: nil)
}
这是升级了手机系统(iOS14)后的样子:
很显然这在新的系统上的变了样子,通过在查找及测试,最后发现确实是UIDatePickerStyle在不同的系统(iOS 13.4以上)下造成的
@available(iOS 13.4, *)
public enum UIDatePickerStyle : Int {
/// Automatically pick the best style available for the current platform & mode.
case automatic = 0
/// Use the wheels (UIPickerView) style. Editing occurs inline.
case wheels = 1
/// Use a compact style for the date picker. Editing occurs in an overlay.
case compact = 2
/// Use a style for the date picker that allows editing in place.
@available(iOS 14.0, *)
case inline = 3
}
End:给UIDatePicker添加UIDatePickerStyle属性值就
---
if #available(iOS 13.4, *) {
datePicker.preferredDatePickerStyle = .wheels
}
---
关于UIDatePicker的其他样式和属性,自己可以试试,比如:
datePicker.preferredDatePickerStyle = .inline
datePicker.preferredDatePickerStyle = . compact