iOS 14 UIDatePicker 在 13.4 新增了2個屬性如下
@property (nonatomic, readwrite, assign) UIDatePickerStyle preferredDatePickerStyle API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);
@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);
原來的界面選擇器樣式就變成新的樣式,默認會設置成UIDatePickerStyleAutomatic,可以看一下注釋Use the wheels (UIPickerView) style. Editing occurs inline.
顯示的時候是上面的,選擇日期的時候成日歷控件了
那怎么才能變成原來的樣式呢?
直接上代碼
if (@available(iOS 13.4, *))
{ _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//新發現這里不會根據系統的語言變了
_datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels; }
else
{ // Fallback on earlier versions }
根據項目中的界面樣式,發現了2個問題,
1.UIDatePicker 設置背景色沒有用了,是透明的。
2.語言不會根據系統語言變化了,要自己設置為中文。
我們會發現確實是之前的滾動齒輪樣式了,但是位置不是屏幕寬度了,這里我們需要在樣式后面重新設置一下UIDatePicker的frame
//之前在這里設置frame是沒問題的,但是iOS13.4之后就沒效果了
self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0,40, KscreenWidth, 230)];
//設置日期選擇器的樣式
if (@available(iOS 13.4, *))
{
self.datePicker.preferredDatePickerStyle=UIDatePickerStyleWheels;
} else {
// Fallback on earlier versions
}
//注意日期選擇器的frame需要在設置樣式后重新設置,在上面設置frame是沒效果的
[self.datePicker setFrame:CGRectMake(0,40, KscreenWidth, 230)];
效果如下