iOS 14 UIDatePicker適配問題,使用老的選擇器樣式。

2020-09-20 11:00:59

iOS 14 UIDatePicker 在 13.4 新增了2個屬性如下

/// Request a style for the date picker. If the style changed, then the date picker may need to be resized and will generate a layout pass to display correctly.
@property (nonatomic, readwrite, assign) UIDatePickerStyle preferredDatePickerStyle API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);

/// The style that the date picker is using for its layout. This property always returns a concrete style (never automatic).
@property (nonatomic, readonly, assign) UIDatePickerStyle datePickerStyle API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);
typedef NS_ENUM(NSInteger, UIDatePickerStyle) {
    /// Automatically pick the best style available for the current platform & mode.
    UIDatePickerStyleAutomatic,
    /// Use the wheels (UIPickerView) style. Editing occurs inline.
    UIDatePickerStyleWheels,
    /// Use a compact style for the date picker. Editing occurs in an overlay.
    UIDatePickerStyleCompact,
    /// Use a style for the date picker that allows editing in place.
    UIDatePickerStyleInline API_AVAILABLE(ios(14.0)) API_UNAVAILABLE(tvos, watchos),
} API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);

原來的介面選擇器樣式就變成新的樣式
在這裡插入圖片描述
那怎麼才能變成原來的樣式呢?

直接上程式碼

            //_datePicker.backgroundColor = [UIColor whiteColor]; 背景色是透明的。
            if (@available(iOS 13.4, *)) {
                _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//新發現這裡不會根據系統的語言變了
                _datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
            } else {
                // Fallback on earlier versions
            }

根據專案中的介面樣式,發現了2個問題,
1.UIDatePicker 設定背景色沒有用了,是透明的。
2.語言不會根據系統語言變化了,要自己設定為中文。

修復調整後效果如下
在這裡插入圖片描述