xcode 13更新后出现的bug

2021-12-22  本文已影响0人  哎呦我去叫什么呢

UITableView sectionHeader下移22像素?

swift

if#available(iOS15.0, *) {

            self.mainTableView.sectionHeaderTopPadding =0        }

OC
 if(@available(iOS15.0, *)) {

            _downloadTableView.sectionHeaderTopPadding = 0;

        }

导航背景颜色修改无效?

swift

if #available(iOS 15.0, *) {

            let appearance = UITabBarAppearance()

            appearance.configureWithOpaqueBackground()

            if darkMode {

                appearance.backgroundColor = UIColor.darkBackgroundColour

            } else {

                appearance.backgroundColor = .white

            }

            appearance.shadowImage = nil

  appearance.shadowColor = nil

            appDelegate.tabBarController.tabBar.standardAppearance = appearance

            appDelegate.tabBarController.tabBar.scrollEdgeAppearance = appDelegate.tabBarController.tabBar.standardAppearance

        }



OC

if (@available(iOS 15.0, *)) {

        UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];

        barApp.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];

        self.navigationController.navigationBar.scrollEdgeAppearance = barApp;

        self.navigationController.navigationBar.standardAppearance = barApp;
}

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

解决方法

主要是以下两个属性 (UINavigationController 的属性)

// 静止样式
self.navigationBar.standardAppearance;
// 滚动样式
self.navigationBar.scrollEdgeAppearance;
swift
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
              // 设置导航栏背景色
            appearance.backgroundColor = .white
              // 去除导航栏阴影(如果不设置clear,导航栏底下会有一条阴影线)
            appearance.shadowColor = UIColor.clear
              // 字体颜色、尺寸等
            appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            // 带scroll滑动的页面
            navigationController?.navigationBar.scrollEdgeAppearance = appearance
            // 常规页面
            navigationController?.navigationBar.standardAppearance = appearance 
        }

OC 
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance * appearance = [[UINavigationBarAppearance alloc] init];
        // 背景色
        appearance.backgroundColor = [UIColor whiteColor];
        // 去除导航栏阴影(如果不设置clear,导航栏底下会有一条阴影线)
        appearance.shadowColor = [UIColor clearColor];
        // 设置字体颜色、尺寸等
        appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor redColor]};
        // 带scroll滑动的页面
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
        // 常规页面
        self.navigationController.navigationBar.standardAppearance = appearance;
    }

导航栏隐藏的返回按钮失效问题
swift

//appdelegate全局设置
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-200, 0) forBarMetrics:UIBarMetricsDefault];
    // iOS 15适配
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance setBackgroundColor:[UIColor whiteColor]];
        // UINavigationBarAppearance 会覆盖原有的导航栏设置,这里需要重新设置返回按钮隐藏,不隐藏可注释或删掉
        appearance.backButtonAppearance.normal.titlePositionAdjustment = UIOffsetMake(-200, 0);

        [[UINavigationBar appearance] setScrollEdgeAppearance: appearance];
        [[UINavigationBar appearance] setStandardAppearance:appearance];
    }

UITabbar
tabbar 背景颜色设置失效

        if #available(iOS 13.0, *) { 
            let appearance = UITabBarAppearance()
           // 背景色
            appearance.backgroundColor = .white
            tabBar.standardAppearance = appearance
            if #available(iOS 15.0, *) {
                tabBar.scrollEdgeAppearance = appearance
            }
        } 

OC

    if (@available(iOS 13.0, *)) { 
        UITabBarAppearance * appearance = [[UITabBarAppearance alloc] init];
         // 背景色
        appearance.backgroundColor = [UIColor whiteColor];
        self.tabBar.standardAppearance = appearance; 
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = appearance;
        }
    }

iOS 15 的 UITableView 新增了一条新属性:sectionHeaderTopPadding, 默认会给每一个 section header 增加一个高度,当我们使用 UITableViewStylePlain 初始化 UITableView 的时候,能发现 sectionHeader 增高了 22px。

/// Padding above each section header. The default value is `UITableViewAutomaticDimension`.
    @available(iOS 15.0, *)
    open var sectionHeaderTopPadding: CGFloat
    
//iOS 15中tableView会给每一个section的顶部(header以上)再加上一个22像素的高度,形成一个section和section之间的间距

  if #available(iOS 15.0, *) {
      tableView.sectionHeaderTopPadding = 0
  }

//或者appdelegate全局设置
  if #available(iOS 15.0, *) { 
     UITableView.appearance().sectionHeaderTopPadding = 0
   }
OC
    if (@available(iOS 15.0, *)) { 
        tableView.sectionHeaderTopPadding = 0;
    }

 //或者appdelegate全局设置 
    if (@available(iOS 15.0, *)) {
        [UITableView appearance].sectionHeaderTopPadding = 0;
    }

原文链接:https://www.jianshu.com/p/10c7921e6d76

上一篇下一篇

猜你喜欢

热点阅读