Change status bar color dynamically in Swift 4

后端 未结 3 1100
天命终不由人
天命终不由人 2021-01-31 22:11

I would like to change the status bar color between .lightContent and .default dynamically (since my background can change in the same ViewController).

3条回答
  •  深忆病人
    2021-01-31 22:46

    I hope below code will use for your need (for Swift4):

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView
        if statusBar?.responds(to: #selector(setter: UIView.backgroundColor)) ?? false {
            statusBar?.backgroundColor = .green
        }
    }
    

    Alternatively, you could try this:

       if let StatusbarView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
                    StatusbarView.backgroundColor = .green
       }
    

提交回复
热议问题