iOS Custom Status Bar Background Color not display

2019-01-24 02:40发布

I am trying to fill the status bar background color to orange using the following

UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

However, I get a white status bar that should be filled with orange instead from following this example: Customize navigation bar appearance with swift

I am setting this up in the AppDelegate.swift file under didFinishLaunchingWithOptions method to apply it to the entire app.

I have edited my info.plist to the following: View controller-based status bar appearance => NO

Does anyone know what I am doing wrong?

Edit: I'm not sure if it matters but the view is in a UITabBarController

Edit 2: This is happening in all the views actually, not just the UITabBarController.

Edit 3: Thanks @Utsav Parikh

I am adding a view now on top of the status bar and it for a brief moment while the app loads the status bar is orange but, once it finishes loading it gets pushed OFF the view and replaced with the generic white status bar. Why would this be happening?

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
view.backgroundColor=UIColor.orangeColor()
self.window!.rootViewController!.view.addSubview(view) 

Edit for Swift 3:

with UITabBarController

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)

Without embedded controllers

I realize some people come here not only for the status bar, but actually the navigation bar, so I learned a few tricks along the way to do it without any embedded controllers:

Add this method in your AppDelegate.swift and call it in the didFinishLaunchingWithOptions

func customizeAppearance() {
    UINavigationBar.appearance().barTintColor = UIColor.black
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    UITabBar.appearance().barTintColor = UIColor.black
    let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

    UITabBar.appearance().tintColor = tintColor
}

9条回答
做自己的国王
2楼-- · 2019-01-24 03:31

Swift 3:

In your AppDelegate.swift file paste the code bellow into your didFinishLaunchingWithOptions method:

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = UIColor(red: 255/255, green: 130/255, blue: 0/255, alpha: 1.0) // Organge colour in RGB
self.window?.rootViewController?.view.addSubview(view)

This works fine for me!

查看更多
戒情不戒烟
3楼-- · 2019-01-24 03:33

I think your last line is reverting your changes, try this:

override func viewWillAppear(animated: Bool) {
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
        super.viewWillAppear(animated)
        var nav = self.navigationController?.navigationBar
        nav?.barStyle = UIBarStyle.Black
        nav?.tintColor = UIColor.orangeColor()
        nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    }
查看更多
时光不老,我们不散
4楼-- · 2019-01-24 03:34

Simon's answer in swift 3

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)

There is one other way I know which uses private api. This has some benefits when orientation changes and keyboard is presented and view move up. I've used it and was lucky every time (app was released in the app store).

func setStatusBarBackgroundColor(color: UIColor) {

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

    statusBar.backgroundColor = color
} 
查看更多
登录 后发表回答