iOS 15 中的状态栏样式不变

Posted

技术标签:

【中文标题】iOS 15 中的状态栏样式不变【英文标题】:Status bar style not changing in iOS 15 【发布时间】:2021-12-27 10:57:34 【问题描述】:

我想根据屏幕内容更改状态栏样式。对于较暗的屏幕,状态栏内容应为白色。对于较亮的屏幕,状态栏内容应为黑色。

这个问题似乎只出现在 ios 15 设备上。

下面的屏幕截图比较了 iOS 15.2 和 iOS 14.5 上的相同用例

如您所见,即使导航栏样式设置为“黑色”,状态栏样式也是默认的

class ViewAController: UIViewController 
 
    override func viewDidLoad() 
        super.viewDidLoad()
        
        // setup views ...
    
    
     // setting navigation bar style doesn't work iOS 15. Needed to set UINavigationAppearance()
    override func viewWillAppear(_ animated: Bool) 
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = .white
        self.navigationController?.navigationBar.barStyle = .default
        self.navigationController?.navigationBar.titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 18), .foregroundColor: UIColor.black]
        
        if #available(iOS 15, *) 
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .white
            appearance.shadowImage = UIImage()
            appearance.shadowColor = .clear
            
            appearance.titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 18), .foregroundColor: UIColor.black]
            
            self.navigationItem.standardAppearance = appearance
            self.navigationItem.scrollEdgeAppearance = appearance
        
    


    @objc private func onClick(_ sender: UIButton) 
        let vc = ViewBViewController()
        self.show(vc, sender: self)
    



class ViewBViewController: UIViewController 
    
    override func viewDidLoad() 
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        self.navigationItem.title = "View B"
    
    
    override func viewWillAppear(_ animated: Bool) 
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.barTintColor = .black
        self.navigationController?.navigationBar.barStyle = .black
        self.navigationController?.navigationBar.titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 18), .foregroundColor: UIColor.white]
        
        // setting navigation bar style doesn't work iOS 15. Needed to set UINavigationAppearance()
        if #available(iOS 15, *) 
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .black
            appearance.shadowImage = UIImage()
            appearance.shadowColor = .clear
            
            appearance.titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 18), .foregroundColor: UIColor.white]
            
            self.navigationItem.standardAppearance = appearance
            self.navigationItem.scrollEdgeAppearance = appearance
        
        
    

【问题讨论】:

覆盖 var preferredStatusBarStyle: UIStatusBarStyle .lightContent 导航栏样式是死信。它不再重要,也可能不存在。这是因为状态栏内容主要响应用户风格(浅色模式或深色模式)。 @BenRockey 当 VC 使用 UINavigationController 包装时它不起作用。如果 VC 在 Navigation Controller 中,设置 preferredStatusBarStyle 不会改变状态栏样式。 @matt 有没有我在不设置 barStyle 的情况下更改状态栏主题?我已经尝试过preferredStatusBarStyle,如果VCs被包裹在UINavigationController中,它就不起作用了。 一个常见的策略是继承 UINavigationController 以便它咨询顶视图控制器。 【参考方案1】:

使用此功能:

extension ViewBViewController 
    override var preferredStatusBarStyle: UIStatusBarStyle 
        .lightContent
    

【讨论】:

不幸的是,这在与 UINavigationController 一起使用时不起作用。

以上是关于iOS 15 中的状态栏样式不变的主要内容,如果未能解决你的问题,请参考以下文章

Swift 4 - 特定视图控制器状态栏样式不变

ios设置状态栏样式

iOS 14、15苹果手机状态栏高度

android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)

StatusBar 仅在 iOS 15 中忽略 .lightContent 样式

如何在 iOS 7 启动期间更改状态栏样式