Change Status Bar Background Color in Swift 3

前端 未结 14 1550
挽巷
挽巷 2020-11-28 04:21

In XCode 7.3.x ill changed the background Color for my StatusBar with:

func setStatusBarBackgroundColor(color: UIColor) {
guard  let statusBar = UIApplicatio         


        
相关标签:
14条回答
  • 2020-11-28 05:00

    I made this extension to change color of status bar. It's not dependent on the key. So it is much safer to use

    public extension UIViewController {
        func setStatusBar(color: UIColor) {
            let tag = 12321
            if let taggedView = self.view.viewWithTag(tag){
                taggedView.removeFromSuperview()
            }
            let overView = UIView()
            overView.frame = UIApplication.shared.statusBarFrame
            overView.backgroundColor = color
            overView.tag = tag
            self.view.addSubview(overView)
        }
    }
    

    Here is usage anywhere in viewcontroller:

    setStatusBar(color: .red)

    0 讨论(0)
  • 2020-11-28 05:00

    Add following code in your extension file to edit your status bar in swift 4 and Above:

    extension UIApplication {
        var statusBarView: UIView? {
            if responds(to: Selector(("statusBar"))) {
                return value(forKey: "statusBar") as? UIView
            }
            return nil
        }
    }
    

    now, we can edit status bar by adding following line in our ViewController class:

    UIApplication.shared.statusBarView?.backgroundColor = <Your Color name>
    
    ex: UIApplication.shared.statusBarView?.backgroundColor = .red
    

    Hope, this will be helpful. Thanks

    0 讨论(0)
提交回复
热议问题