iOS15 适配更新总结

作者: smile丽语 | 来源:发表于2021-10-12 18:09 被阅读0次

    本文主要分享一下 iOS15 上适配方案,仅做开发记录使用,开发过程中通过使用陆续增加。

    iOS15 的适配,很重要的一环就集中在UINavigationBar和UITabbar方面。
    用新Xcode13编译工程后,iOS15项目显示出现视图问题。

    iOS15 适配
    1、UINavigationBar
    2、UITabBar
    3、TableView
    4、Image

    ▐ UINavigationBar

     从 iOS 15 开始,UINavigationBar在控制器中关联滚动视图顶部使用;
    在iOS15中,UINavigationBar默认是透明的,有滑动时会逐渐变为模糊效果,可以通过改变UINavigationBar.scrollEdgeAppearance属性直接变为模糊效果、配置相关属性-背景、字体等

    现有问题:
    用新Xcode13编译iOS15项目后,导航栏的问题比较明显,调试之后发现是UINavigationBar部分属性的设置在iOS15上是无效的。运行起来后发现,导航栏颜色设置失效,字体颜色也失效,并且有导航栏阴影黑线。

    查看导航栏的相关API:
    UINavigationBarAppearance后发现,iOS15navigationBar的相关属性设置要通过实例UINavigationBarAppearance来实现,UINavigationBarAppearance是iOS13更新的API,应该有人已经在用,我们的应用兼容iOS10+,对于导航栏的设置还没有使用UINavigationBarAppearance,如今在iOS15上失效,所以对于呈现的问题,做如下适配:

        // 修改NarBar背景
        if (@available(iOS 15.0, *)) {
            
            UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
            // 背景色
            appearance.backgroundColor = [UIColor blueColor];
            // 去掉半透明效果
            appearance.backgroundEffect = nil;
            // 标题字体颜色及大小
            appearance.titleTextAttributes = @{
                NSForegroundColorAttributeName : [UIColor whiteColor],
                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;
        }
    

    ▐ UITabBar

     从 iOS 15 开始, UITabBar 在控制器中关联滚动视图底部时使用UITabBarAppearance.scrollEdgeAppearance配置相关属性-背景、字体等

    现有问题:
    用新Xcode13编译iOS15项目后,tabbar的问题和navigationBar的问题属于类类似,运行起来后发现,tabbar背景颜色设置失效,字体颜色也失效,并且阴影设置也失效。

    可查看TabBar的相关API:
    UITabBarAppearance后发现,iOS15的tabBar的相关属性设置要通过实例UITabBarAppearance来设置,所以对于呈现的问题,做如下适配:

    // 修改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:KColorFromRGB(0x53A2F8),
                NSFontAttributeName:[UIFont systemFontOfSize:12],
            };
            //tabBaritem title未选中状态颜色
            appearance.stackedLayoutAppearance.normal.titleTextAttributes =  @{
                NSForegroundColorAttributeName:KColorFromRGB(0x7E7E7E),
                NSFontAttributeName:[UIFont systemFontOfSize:12],
            };
            self.tabBar.scrollEdgeAppearance = appearance;
            self.tabBar.standardAppearance = appearance;
    }
    

    ▐ TableView

     从 iOS 15 开始,TableView 增加sectionHeaderTopPadding属性,默认情况sectionHeaderTopPadding会有22个像素的高度,及默认情况,TableView section header增加22像素的高度

    可做如下适配:

        if (@available(iOS 15.0, *)) {
            self.tableView.sectionHeaderTopPadding = 0;
        }
    

    ▐ Image

     在iOS15中,UIImageWriteToSavedPhotosAlbum存储图片之后的回调不再返回图片了,会返回nil,如果在回调方法里面操作image有可能会直接Crash,目前的解决办法声明一个全局image去记录,后面再去操作:

    self.image = savedImage;
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
      
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
        // self.image doing...
    }
    

    相关文章

      网友评论

        本文标题:iOS15 适配更新总结

        本文链接:https://www.haomeiwen.com/subject/krfjoltx.html