「时光不负,创作不停,本文正在参加2021年终总结征文大赛」
大概半年前吧,当时有运营同事反馈说,选择日历弹框出问题了,是一个swift项目,也马上改好了,而且还全局遍历了个遍,把UIDatePicker的问题都修改了。可惜头脑还会慢了,既然这个项目出问题,其它项目就不会了吗?是真的蠢,现在又发现一个OC项目出同样的问题了。我既然这个项目遍历了个遍,其它项目怎么不去检查一下呢,记录下来,引以为戒。
UIDatePicker iOS 13.4 问题
可以看到上面的时间不但偏离位置,而且不能滚动选择了。这是为什么呢
我们看下13.4新增了什么
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:自动选择最佳的风格可用的当前平台和模式。展示的样式就是和上面的图片一样了。
UIDatePickerStyleWheels:使用车轮(UIPickerView)样式。编辑出现内联。
很明显,UIDatePickerStyleWheels才是我们的经典样式。
UIDatePickerStyleCompact:为日期选择器使用紧凑的样式。编辑在覆盖中进行。这个的作用就是点击弹出日历选择框。
UIDatePickerStyleInline
UIDatePickerStyleInline是iOS14才有的,和UIDatePickerStyleCompact有点类似的地方,它是直接展示日历在界面上。
这个就是日历左右翻滚效果,左上角显示选择的时间,我没有特意去优化UI,但大概呈现的效果和这个一样。
修复代码
if (@available(iOS 13.4, *)) {
_datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
}
最后贴出一下修改代码,我们用的还是经典展示,所以选择UIDatePickerStyleWheels就好。