早在ios15出来之前,就在预警ios15适配问题,在ios15测试版出来后,升级系统后,采用xcode12.5运行项目,导航栏没问题,但使用xcode13运行项目,跑在ios15系统的设备上,发现导航栏背景色失效,现对于该问题处理做一下记录

<>NavigationBar背景色失效

原代码:设置背景色,使用颜色生成图片,设置背景图片
[self.navigationController.navigationBar setBackgroundImage:[ImageTool
creatImageAccordingToColor:color] forBarMetrics:UIBarMetricsDefault];
苹果论坛说明

As of iOS 15, UINavigationBar, UIToolbar, and UITabBar will use their
scrollEdgeAppearance when your view controller’s associated scroll view is at
the appropriate edge (or always if you don’t have a UIScrollView in your
hierarchy, more on that below).
译:从 iOS 15 开始,UINavigationBar、UIToolbar 和 UITabBar
将在您的视图控制器的关联滚动视图位于适当的边缘时使用它们的 scrollEdgeAppearance(或者总是如果您的层次结构中没有
UIScrollView,更多内容见下文)。

You must adopt the UIBarAppearance APIs (available since iOS 13,
specializations for each bar type) to customize this behavior. UIToolbar and
UITabBar add scrollEdgeAppearance properties for this purpose in iOS 15.
译:您必须采用 UIBarAppearance API(自 iOS 13 起可用,针对每种条形类型进行了专门化)来自定义此行为。 UIToolbar 和
UITabBar 为此在 iOS 15 中添加了 scrollEdgeAppearance 属性。

所以,代码更改如下:
if (@available(iOS 15.0, *)) { UINavigationBarAppearance *barApp = [[
UINavigationBarAppearance alloc] init]; barApp.backgroundColor = color; barApp.
shadowColor= [UIColor clearColor]; self.navigationController.navigationBar.
scrollEdgeAppearance= barApp; self.navigationController.navigationBar.
standardAppearance= barApp; } else { [self.navigationController.navigationBar
setBackgroundImage:[ImageTool creatImageAccordingToColor:color] forBarMetrics:
UIBarMetricsDefault]; }
注意:这里只说到了设置颜色,如果你的需求,还需要设置字体颜色等,也要设置UINavigationBarAppearance的对应的属性
比如:
if (@available(iOS 15.0, *)) { UINavigationBarAppearance *barApp = [[
UINavigationBarAppearance alloc] init]; barApp.backgroundColor = color; barApp.
shadowColor= [UIColor clearColor]; barApp.titleTextAttributes = @{
NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont
systemFontOfSize:15 weight:UIFontWeightRegular]}; self.navigationController.
navigationBar.scrollEdgeAppearance = barApp; self.navigationController.
navigationBar.standardAppearance = barApp; }
<>UITableView section增加默认高度

UITableView又新增了一个新属性:sectionHeaderTopPadding 会给每一个section header
增加一个默认高度,当我们使用UITableViewStylePlain 初始化tableView的时候,就会发现系统默认给section
header增高了22像素。
所以,添加一句代码:
if (@available(iOS 15.0, *)) { _tableView.sectionHeaderTopPadding = 0; }
//或者全局适配 if #available(iOS15.0,*) { UITableView.appearance().
sectionHeaderTopPadding=0 }

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信