苹果规定2022年4月份之后,所有的app必须使用xcode13进行构建;但是老项目在Xcode13打包iOS15系统UI展示错乱,所以适配iOS15势在必行,以下是适配时的总结的几点经验,目前在适配阶段后续有新增会同步更新,有不对的地方希望各位大佬指出哈,仅供参考。
1、导航栏UINavigationBar
用新xcode13编译工程后,导航栏的问题比较明显,调试之后发现是UINavigationBar部分属性的设置在iOS15上是无效的,运行后发现,导航栏颜色设置没有作用,呈现是白色,字体颜色也没有生效,呈现黑色,查看系统API后发现:从 iOS 15 开始,UINavigationBar、UIToolbar 和 UITabBar 在控制器中关联滚动视图顶部或底部时使用,iOS15 navigationBar的相关属性设置要通过实例UINavigationBarAppearance来实现,在iOS15中,UINavigationBar默认是透明的,有滑动时会逐渐变为模糊效果,可以通过改变UINavigationBar.scrollEdgeAppearance属性直接变为模糊效果、配置相关属性-背景、字体等。适配如下:
// 修改NarBar背景
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
// 背景色
appearance.backgroundColor = [UIColor whiteColor];
// 去掉半透明效果
appearance.backgroundEffect = nil;
// tabBaritem title选中状态颜色
appearance.titleTextAttributes = @{
NSForegroundColorAttributeName : [UIColor jmTextColor],
NSFontAttributeName : [UIFont boldSystemFontOfSize:18],
};
// 设置导航栏下边界分割线透明
appearance.shadowImage = [[UIImage alloc] init];
// 去除导航栏阴影(如果不设置clear,导航栏底下会有一条阴影线)
appearance.shadowColor = [UIColor clearColor];
// standardAppearance:常规状态, 标准外观,iOS15之后不设置的时候,导航栏背景透明
self.navigationBar.standardAppearance = appearance;
// scrollEdgeAppearance:被scrollview向下拉的状态, 滚动时外观,不设置的时候,使用标准外观
self.navigationBar.scrollEdgeAppearance = appearance;
}
2、TableView
从 iOS 15 开始,TableView 增加sectionHeaderTopPadding属性,默认情况sectionHeaderTopPadding会有22个像素的高度,及默认情况,TableView section header增加22像素的高度。这会导致使用Xcode13编译时列表间隔变大,所以为了避免页面凌乱问题做如下适配:
if (@available(iOS 15.0, *)) {
self.sectionHeaderTopPadding = 0;
}
3、UImage
问题一:在iOS15中,UIImageWriteToSavedPhotosAlbum存储图片之后的回调不再返回图片了,会返回nil,如果在回调方法里面操作image有可能会直接Crash,目前的解决办法声明一个全局image去记录,后面再去操作
UIImageWriteToSavedPhotosAlbum(self.imageDetailView.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
}
问题二:升级iOS15系统后,即使是低版本打的包也会出现截图被自动放大的问题,很多商家对此问题进行了反馈,发现iOS15可能对PHImageContentMode做了调整所以做一下适配:把PHImageContentModeAspectFill 改为 PHImageContentModeAspectFit
问题三:UImage新增一下方法
//异步同步准备此图像以在指定屏幕上显示
/// Synchronously prepares this image for displaying on the specified screen.
- (nullable UIImage *)imageByPreparingForDisplay API_AVAILABLE(ios(15.0),tvos(15.0),watchos(8.0));
/// Asynchronously prepares this image for displaying on the specified screen.
- (void)prepareForDisplayWithCompletionHandler:(void (^)(UIImage *_Nullable))completionHandler NS_SWIFT_ASYNC_NAME(byPreparingForDisplay()) API_AVAILABLE(ios(15.0),tvos(15.0),watchos(8.0));
//调整尺寸
- (nullable UIImage *)imageByPreparingThumbnailOfSize:(CGSize)size API_AVAILABLE(ios(15.0),tvos(15.0),watchos(8.0));
- (void)prepareThumbnailOfSize:(CGSize)size completionHandler:(void (^)(UIImage *_Nullable))completionHandler NS_SWIFT_ASYNC_NAME(byPreparingThumbnail(ofSize:)) API_AVAILABLE(ios(15.0),tvos(15.0),watchos(8.0));
4、UITabbar
用新Xcode13编译iOS15项目后,tabbar的问题和navigationBar的问题属于类类似,运行起来后发现,tabbar背景颜色设置失效,字体颜色也失效,并且阴影设置也失效。查看TabBar的相关API:从 iOS 15 开始, UITabBar 在控制器中关联滚动视图底部时使用UITabBarAppearance.scrollEdgeAppearance配置相关属性-背景、字体等,所以做如下适配:
// 修改tabbar背景
if (@available(iOS 15.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
//tabBar背景颜色
appearance.backgroundColor = [UIColor whiteColor];
// 去掉半透明效果
appearance.backgroundEffect = nil;
// tabBaritem title选中状态颜色
appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{
NSForegroundColorAttributeName:[UIColor blueColor],
NSFontAttributeName:[UIFont systemFontOfSize:12],
};
//tabBaritem title未选中状态颜色
appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{
NSForegroundColorAttributeName:[UIColor grayColor],
NSFontAttributeName:[UIFont systemFontOfSize:12],
};
self.tabBar.scrollEdgeAppearance = appearance;
self.tabBar.standardAppearance = appearance;
}
5、UIButton
iOS15支持更多配置。
新增一:UIButtonConfiguration是一个新类,它指定按钮及其内容的外观和行为。它有许多与按钮外观和内容相关的属性,如cornerStyle、baseForegroundColor、baseBackgroundColor、buttonSize、title、image、subtitle、titlePadding、imagePadding、contentInsets、imagePlacement等。
\
6、CLLocationButton
推出CLLocationButton用于一次性定位授权,该内容内置于CoreLocationUI模块,但如果需要获取定位的详细信息仍然需要借助于CoreLocation。
API_AVAILABLE(ios(15.0),watchos(8.0))
@interface CLLocationButton : UIControl <NSSecureCoding>
@property (nonatomic, readwrite) CLLocationButtonIcon icon;
@property (nonatomic, readwrite) CLLocationButtonLabel label;
@property (nonatomic, readwrite) CGFloat fontSize;
@property (nonatomic, readwrite) CGFloat cornerRadius;
@end
7、UISheetPresentationController
iOS15新增UISheetPresentationController让您将视图控制器呈现为工作表。在展示您的视图控制器之前,请在其属性中配置工作表展示控制器,使其具有您想要的工作表的行为和外观。
\
继承关系UISheetPresentationController->UIPresentationController -> NSObject
图片选择使用案例:
效果图: