Change Status Bar Background Color in Swift 3

前端 未结 14 1594
挽巷
挽巷 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 04:40

    Previous Code:-

    func setStatusBarBackgroundColor(color: UIColor) {
            guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
    
            statusBar.backgroundColor = color
        }
    

    My application got a crash show reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

    Updated Code-

      if #available(iOS 13.0, *) {
                let statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
                 statusBar.backgroundColor = UIColor.init(red: 237.0/255.0, green: 85.0/255.0, blue: 61.0/255.0, alpha: 1.0)
                 UIApplication.shared.keyWindow?.addSubview(statusBar)
            } else {
                 UIApplication.shared.statusBarView?.backgroundColor = UIColor.init(red: 237.0/255.0, green: 85.0/255.0, blue: 61.0/255.0, alpha: 1.0)
            }
    

    This code is working swift 5.2

    enter image description here

提交回复
热议问题