iOS 15 中状态栏样式没有改变

The*_*tun 5 uikit uinavigationcontroller swift uinavigationbarappearance ios15

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

看来该问题仅发生在 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)
    }
}

Run Code Online (Sandbox Code Playgroud)

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
        }
        
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

使用这个函数:

extension ViewBViewController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        .lightContent
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,当与 UINavigationController 一起使用时,这不起作用。 (3认同)