Change Status Bar Background Color in Swift 3

前端 未结 14 1549
挽巷
挽巷 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:36

    Try this

    Goto your app info.plist

    1. Set View controller-based status bar appearance to NO
    2. Set Status bar style to UIStatusBarStyleLightContent

    Then Goto your app delegate and paste the following code where you set your Windows's RootViewController.

    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
        view.backgroundColor=[UIColor blackColor];
        [self.window.rootViewController.view addSubview:view];
    }
    
    0 讨论(0)
  • 2020-11-28 04:36

    The possible solution is adding view that will be used as background of statusBar on your viewController:

    let frame = screenController.view.convert(UIApplication.shared.statusBarFrame, to: screenController.view)
    let backview = UIView(frame: frame)
    
    backview.backgroundColor = backgroundColor
    screenController.view.addSubview(backview)
    screenController.view.bringSubviewToFront(backview)
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-28 04:45

    I have a custom solution for changing status bar on iOS 13 and below. Here is how to do that:

    if #available(iOS 13.0, *) {
       let app = UIApplication.shared
       let statusBarHeight: CGFloat = app.statusBarFrame.size.height
    
       let statusbarView = UIView()
       statusbarView.backgroundColor = UIColor.red
       view.addSubview(statusbarView)
    
       statusbarView.translatesAutoresizingMaskIntoConstraints = false
       statusbarView.heightAnchor
         .constraint(equalToConstant: statusBarHeight).isActive = true
       statusbarView.widthAnchor
         .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
       statusbarView.topAnchor
         .constraint(equalTo: view.topAnchor).isActive = true
       statusbarView.centerXAnchor
         .constraint(equalTo: view.centerXAnchor).isActive = true
    
    } else {
          let statusBar = UIApplication.shared.value(forKeyPath: 
       "statusBarWindow.statusBar") as? UIView
          statusBar?.backgroundColor = UIColor.red
    }
    

    Gist

    Also, check the article iOS 13 How to Change StatusBar Color?

    One last thing, you can still change statusbar style with :

     override var preferredStatusBarStyle : UIStatusBarStyle {
        return UIStatusBarStyle.lightContent
        //return UIStatusBarStyle.default   // Make dark again
    }
    
    0 讨论(0)
  • 2020-11-28 04:46

    For iOS 11 and Xcode 9 use the following steps.

    1. create an extention to UIApplication class:

      extension UIApplication { var statusBarView: UIView? { return value(forKey: "statusBar") as? UIView } }

    2. In your class or wherever you want to change the Status bar's background color:

      UIApplication.shared.statusBarView?.backgroundColor = .red

    3. For light content or dark content of status bar simply go to Info.plist and add the following value row with value NO.

    View controller-based status bar appearance

    1. Now just set the light content or whatever you need in the General Tab of your project's settings.
    0 讨论(0)
  • 2020-11-28 04:48

    "Change" status bar background color:

    let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
    let statusBarColor = UIColor(red: 32/255, green: 149/255, blue: 215/255, alpha: 1.0)
    statusBarView.backgroundColor = statusBarColor
    view.addSubview(statusBarView)
    

    Change status bar text color:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    Update: please note that the status bar frame will change when the view is rotated. You could update the created subview frame by:

    • Using the autoresizing mask: statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
    • Observing NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
    • Or overriding viewWillLayoutSubviews()
    0 讨论(0)
提交回复
热议问题