iOS14 UIDatePicker的變化
手機系統更新到iOS14之後,在選擇日期的時候變成這樣了
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPRVGawd0YxB3MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL0QTN5QDOwcTM2ATMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
更新之前是這樣的
這是因為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);
}
需要注意的地方是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的變化,大家可以自己動手去試試!