iOS14适配兼容

Cell点击无效
在iOS14上可能出现点击cell上的视图无法响应的情况.
原因:

iOS14更改Cell视图布局.将contentView放在*上层,如果将视图加载在cell上,将会出现contentView遮罩,导致事件无法响应.是在此前关于 contentView 的声明注释中,官方已经明确建议开发者将 customView 放在 contentView 上,使 contentView 作为 UITableViewCell 默认的父视图。

解决办法:
1、可以将cell子视图加载在contentView上(提倡)
2、将contentView设置到*底层 self.sendSubviewToBack(self.contentView)

UIDatePicker 更新 UI 样式
iOS14 UIDatePicker新增加了一个UI样式 UIDatePickerStyleInline 作为默认样式,如果还需要旧版本的滚轮样式,需要设置为

UIDatePicker 的 preferredDatePickerStyle 属性为 UIDatePickerStyleWheels。

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);
相册权限
iOS14 新增了“Limited Photo Library Access” 模式,在授权弹窗中增加了 Select Photo 选项。用户可以在 App 请求调用相册时选择部分照片让 App 读取。从 App 的视⻆来看,你的相册里就只有这几张照片,App 无法得知其它照片的存在。
权限提示框会在每次冷启动后打开相册时重新弹出,可以在 info.plist 中设置 PHPhotoLibraryPreventAutomaticLimitedAccessAlert 选项为 YES ,关闭提示。

在 iOS14 中官方推荐使用 PHPicker 来替代原 API 进行图片选择。PHPicker 为独立进程,会在视图*顶层进行展示,应用内无法对其进行截图也无法直接访问到其内的数据。

UIImagePickerController -> PHPickerViewController, UIImagePickerViewController 功能受限,每次只能选择一张图片,将逐渐被废弃。

地理位置
CLLocationManager 新增了 精确定位 和 模糊定位 的概念,用户可以手动选择,模糊定位的误差约 500m 。可以根据实际功能判断是否可以接受用户选择模糊定位。

如果功能强依赖精确定位,可以在需要的时候调用 requestTemporaryFullAccuracyAuthorizationWithPurposeKey 方法。

– (void)requestTemporaryFullAccuracyAuthorizationWithPurposeKey:(NSString *)purposeKey completion:(void(^ _Nullable)(NSError * _Nullable))completion;

– (void)requestTemporaryFullAccuracyAuthorizationWithPurposeKey:(NSString *)purposeKey;
单独请求一次精确定位,用户可以选择拒*授权。所需参数 purposeKey 需要在 info.plist 中设置 NSLocationTemporaryUsageDescriptionDictionary 字典。

可以通过 accuracyAuthorization 属性获取当前的定位方式:

@property (nonatomic, readonly) CLAccuracyAuthorization accuracyAuthorization;
typedef NS_ENUM(NSInteger, CLAccuracyAuthorization) {
// 精准定位
CLAccuracyAuthorizationFullAccuracy,
// 模糊定位
CLAccuracyAuthorizationReducedAccuracy,
};
适配UIPageControl
iOS14以后删除了pageImage 与currentPageImage,不能再通过KVC修改控制器的图片。

解决方案:

借助iOS14新增字段preferredIndicatorImage,我们可以修改指示器小圆点的大小以及形状,借助currentPageIndicatorTintColor与pageIndicatorTintColor来确认不同状态下小圆点的颜色。

*终展示的颜色是由TintColor以及preferredIndicatorImage共同决定的,因此这里将preferredIndicatorImage设置为白色。

使用layer.mask做遮罩时无法显示
使用UIImageView的layer 做遮罩时,不显示任何东西。

在iOS14以下使用UIImageView的layer做遮罩的代码:

UIImageView *maskView = [[UIImageView alloc] initWithImage:maskImage];
maskView.frame = self.bounds;
self.layer.mask = maskView.layer;
但是在iOS14上无法正常展示。

解决方案:

UIImage *newImage = [self stretchImage:maskImage newSize:self.bounds.size leftCapWidth:maskImage.size.width/2 topCapHeight:maskImage.size.height-7];
CALayer *maskLayer = [CALayer layer];
maskLayer.frame = self.bounds;
maskLayer.contents = (id)newImage.CGImage;
self.layer.mask = maskLayer;
– (UIImage *)stretchImage:(UIImage *)originImage newSize:(CGSize)newSize leftCapWidth:(CGFloat)leftCapWidth topCapHeight:(CGFloat)topCapHeight{
UIImage *newImage;
newImage = [originImage stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
UIGraphicsBeginImageContextWithOptions(newSize, false, 0);
[newImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;