iOS14 UIDatePicker的变化

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的变化,大家可以自己动手去试试!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

短暂又灿烂的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值