iOS15使用prefersLargeTitles时barTin

2023-03-24  本文已影响0人  微步毂纹生

在使用大标题模式(prefersLargeTitles)时,发现无法更改nav的背景颜色,设置barTintColor无效,设置backgroundColor也无效。
查阅发现,iOS15需要使用UINavigationBarAppearance来设置导航栏。
设置后发现滑动失去玻璃效果,这是设置standardAppearance导致。
具体代码如下:
我没有封装navigationController,以下代码是在BaseViewController中设置,如果封装了navigationController,也可以在其中如此设置

    self.view.backgroundColor = [UIColor systemBackgroundColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor systemBackgroundColor];
    self.navigationController.navigationBar.barTintColor = [UIColor systemBackgroundColor];
    self.navigationController.navigationBar.tintColor = MINECOLOR;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor labelColor]}];
    
    self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAutomatic;
    
    if(@available(iOS 15.0, *)){
        UINavigationBarAppearance *newAppearance = [UINavigationBarAppearance new];
        [newAppearance configureWithOpaqueBackground];
        newAppearance.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor labelColor]};
        newAppearance.backgroundColor = [UIColor systemBackgroundColor];
        [newAppearance setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor labelColor]}];
        
//        self.navigationController.navigationBar.standardAppearance = newAppearance; //滑动scroll需要毛玻璃效果不要设置
        
        self.navigationController.navigationBar.scrollEdgeAppearance = newAppearance;
    }
    self.navigationController.navigationBar.translucent = YES;


上一篇 下一篇

猜你喜欢

热点阅读