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条回答
smile是对你的礼貌
2楼-- · 2019-01-24 03:12

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
}

Thanks to @Utsav I added the following subview to my UITabBarController and this seems to be working now:

    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.view.addSubview(view)

The UITabBarController doesn't seem to play well in AppDelegate. If anyone has a better way let me know but, as of now this is the solution I have come around to.

查看更多
等我变得足够好
3楼-- · 2019-01-24 03:24

After what u did in info.plist to the following: View controller-based status bar appearance => NO.

Add this code in AppDelegate.swift file under didFinishLaunchingWithOptions:

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x2E9AFE)

// change navigation item title color
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

and u can select any hex code for ur choice of color..!! Enjoy..!!

Sorry, forgot to use hexcode you will be needing this also so add this code anywhere in your AppDelegate.swift:

func uicolorFromHex(rgbValue:UInt32)->UIColor {

    let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0

    let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0

    let blue = CGFloat(rgbValue & 0xFF)/256.0

    return UIColor(red:red, green:green, blue:blue, alpha:1.0)
}
查看更多
Rolldiameter
4楼-- · 2019-01-24 03:28

Add this code in didFinishLaunchingWithOptions in AppDelegate

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)

Hope it helps you....!!!

查看更多
We Are One
5楼-- · 2019-01-24 03:29

This is how I did it without adding a view in a VC with in a NavBarController

I wanted the color of the status bar to be the same as the VC view color so I just wrote:

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.grayColor()
    self.navigationController?.navigationBar.clipsToBounds = true
}

Try it.

查看更多
淡お忘
6楼-- · 2019-01-24 03:30

There is a main difference in tintColor and changing the background color of UINavigationBar. The best way in my opinion is apply a background image, made by 1 pixel square image of just one color.
Like that:

let tabbarAndNavBarBkg = UIImage(named: "nav_tab")
UINavigationBar.appearance().setBackgroundImage(tabbarAndNavBarBkg, forBarMetrics: .Default) 

Or you can create a category on UIColor to return a UIImage given a UIColor instance, in objC:

+ (UIImage *) imageWithColor:(UIColor*) color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return colorImage;
}
查看更多
够拽才男人
7楼-- · 2019-01-24 03:31

UINavigationBar.appereance() works for upcoming viewControllers, but not the currently displayed rootViewController. To achieve this I have added the following to my didFinishLaunchingWithOptions:

UINavigationBar.appearance().tintColor = myColor

let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController
navigationController.navigationBar.barTintColor = myColor
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : myTextColor]
navigationController.navigationBar.translucent = false

navigationController.setNeedsStatusBarAppearanceUpdate()
查看更多
登录 后发表回答