从 iOS 13 UIDatepicker 到 iOS 14 UIDatepicker 的转换

Conversion from iOS 13 UIDatepicker to iOS 14 UIDatepicker

我目前正在尝试弄清楚如何将代码从 iOS 13 UIDatePicker 更改为带有 (.wheels) 的 iOS 14 版本。您将在下方找到带有无法正常运行的日期选择器的应用程序屏幕截图和代码屏幕截图。

不幸的是,虽然我能够理解 Swift,但我不够熟悉,无法知道需要在何处进行哪些更改才能使代码正常工作。我在这里尝试了其他一些解决方案,但无济于事。所以,如果有人能帮助我,那就太好了。

我认为我需要更改 dateBtnAction 的代码,但我不知道新的 iOS 14 代码会去哪里。

@IBAction func datePickerAction(_ sender: UIDatePicker) {
    let formatter = DateFormatter()
    formatter.dateFormat = "MMM dd, yyyy h:mm a"
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.amSymbol = "am"
    formatter.pmSymbol = "pm"
    // formatter.timeZone = TimeZone(abbreviation: "UTC")!
    // formatter.dateStyle  = .medium
    let dateString = formatter.string(from: sender.date)
    dateBtn.setTitle(dateString, for: .normal)
}

@IBAction func dateBtnAction(_ sender: Any) {
    popupView(forView: datePickerContainerView, viewHeight: 300)
}
Code snippet Result

只需在您 viewDidLoad 或您设置 datePicker 的任何地方添加此代码:

if #available(iOS 13.4, *) {
   yourDatePicker.preferredDatePickerStyle = .wheels
   //yourDatePicker can be in code or IBOutlet

}

根据文档,这些是可用的样式 (iOS 13.4):

Styles

case automatic
A style indicating that the system picks the concrete style based on the current platform and date picker mode.

case compact
A style indicating that the date picker displays as a label that when tapped displays a calendar-style editor.

case inline
A style indicating that the date pickers displays as an inline, editable field.

case wheels
A style indicating that the date picker displays as a wheel picker.