天天看點

iOS14 UIDatePicker的變化

iOS14 UIDatePicker的變化

手機系統更新到iOS14之後,在選擇日期的時候變成這樣了

iOS14 UIDatePicker的變化

 更新之前是這樣的

iOS14 UIDatePicker的變化

這是因為UIDatePicker 增加了pickerStyle,需要設定preferredDatePickerStyle = UIDatePickerStyleWheels才會和以前一樣,并且現在對frame的寬高設定已經不生效了,會采用系統預設的寬高。

preferredDatePickerStyle屬性:

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);
           

解決辦法:

設定frame放到datePickerMode後面設定變成正常了

self.datePicker = [[UIDatePicker alloc] init];
    [self.datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    self.datePicker.datePickerMode=UIDatePickerStyleInline;

	 if (@available(iOS 14.0, *)) {
		self.datePicker.preferredDatePickerStyle = UIDatePickerStyleCompact;
		self.datePicker.frame = CGRectMake(0, 36+50, SCR_W, 60);
	 } else {
		self.datePicker.frame = CGRectMake(0, 36, SCR_W, 200);
	 }
           
iOS14 UIDatePicker的變化

需要注意的地方是preferredDatePickerStyle = UIDatePickerStyleCompact,點選選擇日期會彈出系統自帶的UI樣式,我個人還是比較喜歡的

self.datePicker = [[UIDatePicker alloc] init];
    [self.datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    self.datePicker.datePickerMode=UIDatePickerModeDate;

     if (@available(iOS 14.0, *)) {
        self.datePicker.preferredDatePickerStyle = UIDatePickerStyleCompact;
        self.datePicker.frame = CGRectMake(0, 36+50, SCR_W, 60);
     } else {
        self.datePicker.frame = CGRectMake(0, 36, SCR_W, 200);
     }
           

如下圖點選會彈出系統樣式的日期盤 

iOS14 UIDatePicker的變化

系統樣式的日期盤 

iOS14 UIDatePicker的變化

以上就是iOS14 UIDatePicker的變化,大家可以自己動手去試試!