iOS Xcode 13 navigationBar的背景色失效

1、升级到 Xcode 13 之前写的关于导航条 navigationBar 的颜色设置是直接调用 barTintColor 属性就可以实现。然而到了Xcode 13 颜色设置却失效了。

// iOS 15.0 之前设置 navigationBar 的代码
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[CustomNavigationController class]]];
    bar.barTintColor = [UIColor redColor];
    bar.barStyle = UIBarStyleDefault;
    [bar setTranslucent:NO];   //设置不透明
    bar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], NSFontAttributeName: [UIFont systemFontOfSize:25]};

运行出来导航条显示成了黑色,而不是我这里设置的红色
iOS Xcode 13 navigationBar的背景色失效_第1张图片

2、解决办法

// iOS 15 之后,这样设置导航条的相关属性才正确。之前的做法已失效
    UINavigationBarAppearance *navBarAppearance = [[UINavigationBarAppearance alloc]init];
    [navBarAppearance configureWithOpaqueBackground];
    navBarAppearance.backgroundColor = [UIColor redColor];
    [navBarAppearance setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor blackColor]}];

    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[CustomNavigationController class]]];
    navBar.standardAppearance = navBarAppearance;
    navBar.scrollEdgeAppearance = navBarAppearance;

iOS Xcode 13 navigationBar的背景色失效_第2张图片

你可能感兴趣的:(xcode,ios,objective-c)