简体   繁体   中英

Coloured Status Bar on a TabBarController with NavigationBar not working

I've read a lot of posts on here and tried most of the options mentioned, but none fix the issue for me. I have an app that is based off of a Tab Bar Controller. Each tab is a UIViewController with a Navigation Bar at the top.

Adding this code to the AppDelegate gives me an orange coloured Navigation Bar with white text, but a white status bar with Black text.

[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

Reading the answers on various pages suggest adding the following to the View controller:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

Then calling this in View Did Load:

[self setNeedsStatusBarAppearanceUpdate];

This gets me a White status bar with White text, how can I now get the status bar to go orange to match my Navigation Bar??

The solution mentioned on here https://stackoverflow.com/a/19513714/505457 for those using a Navigation Controller doesn't work, I guess thats because my main controller is a Tab Bar Controller.

Anyone come across this before? Thanks in advance for any advice / suggestions you may have. I can provide a sample app if required, but its probably as quick to build one with the Tab Bar template, add a Navigation bar then paste in my code samples.

Plasma

You can find the statusBar UIVIew by it's name and tint it. Add this method to your AppDelegate.m and call it from didFinishLaunchingWithOptions :

- (void)setStatusBarBackgroundColor:(UIColor *)color {

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 ...   

    [self setStatusBarBackgroundColor:[UIColor orangeColor]];

 ...

}

Note: There are apps in store that use this method. So it is okay with the apple HIG policy.

Well I had almost the same problem since IOS7 , But my quick solution is to change all your views from the first one, I am refering to set up your color bar configuration in the AppDelegate.m file, I recommend to use this free framework Nab Bar Color And Gradient is very easy to use and also you will be available to set a beautiful gradient on all of your views.

See the examples in the project.

Normally, you shouldn't be adding a UINavigationBar to a UIViewController . Instead, fill your UITabBarController will UINavigationController s.

    let firstViewController = FirstViewController(nibName: nil, bundle: nil)
    let firstNavigationController = UINavigationController(rootViewController: firstViewController)

    let secondViewController = SecondViewController(nibName: nil, bundle: nil)
    let secondNavigationController = UINavigationController(rootViewController: secondViewController)

    let navigationControllers = [
        firstNavigationController,
        secondNavigationController
    ]

    yourTabBarController.setViewControllers(navigationControllers, animated: false)

The same general process applies if you are using storyboards.

Right so the point BSmith11 made about adding Navigation controllers to a Tab Bar got me thinking. So I did a bit of Googling and an answer on this page, helps a lot: Tab bar controller inside a uinavigationcontroller

By having the TabBar controller as the rootViewController, then inserting a "NavigationController" between that and the normal ViewController allows me to do this:

    //Fix up the Navigation bar tint and set text colours to white
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor orangeColor]];

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

//Also requires setting 'View controller-based status bar appearance' to NO in .plist
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

Which is exactly what I had before, and that gives me a coloured StatusBar and the exact same colour NavBar. Now to see if I can add it in to the existing app and not break everything.

Plasma

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM