iOS14 UIDatePicker的变化
手机系统更新到iOS14之后,在选择日期的时候变成这样了
更新之前是这样的
这是因为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的变化,大家可以自己动手去试试!