iOS 15的适配

demo:
oc代码:https://github.com/xinsun001/iOS-15UI-oc.git
1、导航栏UINavigationBar

iShot2021-12-15 11.31.04.gif

从 iOS 15 开始,UINavigationBar、UIToolbar 和 UITabBar 在控制器中关联滚动视图顶部或底部时使用
在iOS15中,UINavigationBar默认是透明的,有滑动时会逐渐变为模糊效果,可以通过改变UINavigationBar.scrollEdgeAppearance属性直接变为模糊效果、配置相关属性-背景、字体等
调整如下:
//强烈建议直接隐藏系统导航栏,使用自定义的导航栏!!
object-c:

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *navBarApperance = [UINavigationBarAppearance new];
       // 颜色
        navBarApperance.backgroundColor = [UIColor redColor];

        //图片
//        navBarApperance.backgroundImage = [UIImage imageNamed:@"bgImage.png"];
//        navBarApperance.backgroundImageContentMode = UIViewContentModeScaleToFill;
        
        
        NSDictionary *dictM = @{ NSForegroundColorAttributeName:[UIColor blackColor]};
        navBarApperance.titleTextAttributes = dictM;
        
        self.navigationController.navigationBar.standardAppearance = navBarApperance;
        self.navigationController.navigationBar.scrollEdgeAppearance = navBarApperance;
    }else{
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
        
//        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bgImage.png"] forBarMetrics:UIBarMetricsDefault];
    }

swift:

        if #available(iOS 15.0,*) {
            let navBarAppecrace = UINavigationBarAppearance()
            
            //颜色
//            navBarAppecrace.backgroundColor = UIColor.red
            
            //图片
            navBarAppecrace.backgroundImage = UIImage.init(named: "bgImage.png")
            navBarAppecrace.backgroundImageContentMode = .scaleToFill

            
            navBarAppecrace.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
            
            self.navigationController?.navigationBar.standardAppearance = navBarAppecrace
            self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppecrace
        } else {
            
            self.navigationController?.navigationBar.barTintColor = UIColor.red
            
//            self.navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: "bgImage.png"), for: .any, barMetrics: .default)
        }
        

效果对比:


iShot2021-12-15 11.57.58.gif

image.png

2、Tableview

iShot2021-12-15 12.07.14.gif

因为iOS 15新增sectionHeaderTopPadding属性,在tableview的UITableViewStylePlain类型中,header上方又增加了高度(22像素)
调整如下:
object-c:

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        if (@available(iOS 15.0, *)) {
            _tableview.sectionHeaderTopPadding = 0;  //去掉headerpadding的高度
        }
    }
    return _tableview;
}

swift:

    private lazy var  tableview:UITableView = {
        let tab = UITableView.init(frame: .zero, style: .plain)
        tab.backgroundColor = UIColor.white
        tab.delegate = self
        tab.dataSource = self
        if #available(iOS 15.0, *) {
            tab.sectionHeaderTopPadding = 0
        }
        return tab
    }()

效果对比:

iShot2021-12-15 12.12.26.gif

3:TabBar
这里我看到好多文章说和navigationbar是一个毛病,背景色和图片设置在iOS 15上面都不会生效,但是我经过测试,oc是没有毛病的!swift语言确实会不生效。为了统一做了如下改动
代码和效果图如下:
object-c:

if (@available(iOS 15.0, *)) {
        UITabBarAppearance *tabBarAppearanc = [UITabBarAppearance new];
        
        //颜色
        tabBarAppearanc.backgroundColor = [UIColor greenColor];
        
//        //图片
//        tabBarAppearanc.backgroundImage = [UIImage imageNamed:@"bgImage.png"];
//        tabBarAppearanc.backgroundImageContentMode = UIViewContentModeScaleToFill;
        
        
        NSDictionary *dictM = @{ NSForegroundColorAttributeName:[UIColor redColor]};
        tabBarAppearanc.stackedLayoutAppearance.selected.titleTextAttributes = dictM;
//        tabBarAppearanc.stackedLayoutAppearance.normal.titleTextAttributes = .....

        self.tabBar.scrollEdgeAppearance = tabBarAppearanc;
        self.tabBar.standardAppearance = tabBarAppearanc;
    } else {
        // Fallback on earlier versions
        
        [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
        
        //颜色
        [[UITabBar appearance] setBarTintColor:[UIColor greenColor]];

        //图片
    //    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"bgImage.png"]];
    }

swift:

        if #available(iOS 15.0,*) {
            
            let tabBarAppearanc = UITabBarAppearance.init()
            
            //颜色
//            tabBarAppearanc.backgroundColor = UIColor.green
            
            //图片
            tabBarAppearanc.backgroundImage = UIImage.init(named: "bgImage.png")
            
            
            tabBarAppearanc.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
//            tabBarAppearanc.stackedLayoutAppearance.normal.titleTextAttributes = .....
            
            self.tabBar.standardAppearance = tabBarAppearanc
            self.tabBar.scrollEdgeAppearance = tabBarAppearanc
            
            
        }else{
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
            
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)
            
            //颜色
            UITabBar.appearance().barTintColor = UIColor.green
            
            //图片
//            UITabBar.appearance().backgroundImage = UIImage.init(named: "bgImage.png")
        
        }
        

左边是iOS 15,右边是iOS 14

iShot2021-12-15 14.01.05.gif

4:UIButton
image.png

之前我们可能要自定义一个类来实现这种按钮,但是现在苹果新增了特性
object-c:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor greenColor];
    if (@available(iOS 15.0, *)) {
        UIButtonConfiguration *conf = [UIButtonConfiguration tintedButtonConfiguration];
        conf.cornerStyle = UIButtonConfigurationCornerStyleMedium;
        [conf setImagePadding:5];
        [conf setTitle:@"大标题"];
        [conf setSubtitle:@"副标题"];
        [conf setImage:[UIImage imageNamed:@"btnImage.png"]];
        conf.imagePlacement = NSDirectionalRectEdgeLeading;
        button.configuration = conf;
    } else {
        // Fallback on earlier versions
    }

swift:

        let button:UIButton = UIButton.init(type: .custom)
        button.backgroundColor = UIColor.green
        if #available(iOS 15.0, *) {
            var conf = UIButton.Configuration.tinted()
            conf.cornerStyle = UIButton.Configuration.CornerStyle.medium
            conf.imagePadding = 5
            conf.title = "大标题"
            conf.subtitle = "副标题"
            conf.image = UIImage.init(named: "btnImage.png")
            conf.imagePlacement = .leading
            button.configuration = conf
        }else{
            
        }

WeChat2d513139892f4b37509169da2df8eece.png

5:UIImage
图片的尺寸变换
object-c:

    UIImage *modeImg = [UIImage imageNamed:@"bgImage.png"];
    NSLog(@"图片原尺寸%@",NSStringFromCGSize(modeImg.size));
    
    if (@available(iOS 15.0, *)) {
        modeImg = [modeImg imageByPreparingThumbnailOfSize:CGSizeMake(220, 100)];
        NSLog(@"**111111**变换后图片原尺寸%@",NSStringFromCGSize(modeImg.size));

        [modeImg prepareThumbnailOfSize:CGSizeMake(220, 100) completionHandler:^(UIImage * _Nullable) {
            NSLog(@"###222###变换后图片原尺寸%@",NSStringFromCGSize(modeImg.size));
        }];
    } else {
        // Fallback on earlier versions
    };
//打印值
//2021-12-15 15:48:17.590981+0800 iOS15UI[71637:683127] 图片原尺寸{674, 206}
//2021-12-15 15:48:17.598647+0800 iOS15UI[71637:683127] **111111**变换后图片原尺寸{220, 100}
//2021-12-15 15:48:17.600537+0800 iOS15UI[71637:684293] ###222###变换后图片原尺寸{220, 100}

swift:

        var modImg = UIImage.init(named: "btnImage.png")
        print("原图片尺寸%@",modImg?.size as Any)
        
        if #available(iOS 15.0, *) {
            modImg = modImg?.preparingThumbnail(of: CGSize(width: 220, height: 100))
            print("**111111**变换后图片原尺寸%@",modImg?.size as Any)

            modImg?.prepareThumbnail(of: CGSize(width: 220, height: 100)){img in
                print("###222###变换后图片原尺寸%@",modImg?.size as Any)
            }
        }
//打印值
//原图片尺寸%@ Optional((124.0, 120.0))
//**111111**变换后图片原尺寸%@ Optional((220.0, 100.0))
//###222###变换后图片原尺寸%@ Optional((220.0, 100.0))

其它
UILabel显示的文字比设置font大,在iOS 15中,adjustsFontSizeToFitWidth为true时,高度不能跟设置的 Font 一样大。固定宽度时文字显示不全,之前正好显示完文字,在iOS 15上可能显示不全,增加宽度即可

demo:
oc代码:https://github.com/xinsun001/iOS-15UI-oc.git

罪过,才发现swift的代码上传时只传了文件夹,内容没传上,但是swift的代码我本地已经找不到了,大家对着上面的代码片段将就着看吧!

你可能感兴趣的:(iOS 15的适配)