Swift 专栏iOS 开发每天分享优质文章iOS日常笔记

iOS15适配

2021-12-06  本文已影响0人  Mr_Zhou

用Xcode13编译工程后,需要进行适配(后续遇到继续更新),记录如下:

  1. iOS设置导航栏 NavigationBar
// 修改NarBar背景
    if (@available(iOS 15.0, *)) {
        
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        // 背景色
        appearance.backgroundColor = [UIColor whiteColor];
        // 去掉半透明效果
        appearance.backgroundEffect = nil;
        // 标题字体颜色及大小
        appearance.titleTextAttributes = @{
            NSForegroundColorAttributeName : [UIColor blackColor],
            NSFontAttributeName : [UIFont boldSystemFontOfSize:18],
        };
        // 设置导航栏下边界分割线透明
        appearance.shadowImage = [[UIImage alloc] init];
        // 去除导航栏阴影(如果不设置clear,导航栏底下会有一条阴影线)
        appearance.shadowColor = [UIColor clearColor];
        // standardAppearance:常规状态, 标准外观,iOS15之后不设置的时候,导航栏背景透明
        self.navigationController.navigationBar.standardAppearance = appearance;
        // scrollEdgeAppearance:被scrollview向下拉的状态, 滚动时外观,不设置的时候,使用标准外观
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    }
  1. 适配 TabBar
// 修改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;
}

针对一些控制器在push时隐藏导航栏,在pop时显示导航栏的处理

// 对导航栏的处理
+(void)adaptationNavBar
{
    if (@available(iOS 13.0, *)) {
           
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
          
        [appearance setShadowImage:[[UIImage alloc] init]];
          
        [appearance setBackgroundColor:[UIColor whiteColor]];
          
        // 隐藏分割线 设置一个透明或者纯色的图片 设置nil 或者 [UIImage new]无效
        [appearance setBackgroundImage:[self imageWithColor:[UIColor whiteColor]]];
          
        [appearance setShadowImage:[self imageWithColor:[UIColor whiteColor]]];
          
        [[UINavigationBar appearance] setScrollEdgeAppearance: appearance];
       
    }
}

+(UIImage *)imageWithColor:(UIColor *)color {
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(3, 3), NO, [UIScreen mainScreen].scale);
    
    UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 3, 3)];
   
    [color setFill];
    
    [bezierPath fill];
    
    UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
   
    return img;
}
  1. TableView 增加了sectionHeaderTopPadding属性,TableView section header增加22像素的高度
if (@available(iOS 15.0, *)) {
     self.tableView.sectionHeaderTopPadding = 0;
}

有些界面我直接把导航栏隐藏,自己画了导航栏,这种方法简单粗暴,发现导航栏出现的问题都解决了。um..... 就这么多,有问题请指出。

上一篇 下一篇

猜你喜欢

热点阅读