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的代码我本地已经找不到了,大家对着上面的代码片段将就着看吧!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335

推荐阅读更多精彩内容