Search code examples
iosswiftios9

iOS 9 Navigation Bar Tile Background


I have some problems in XCode. I tried to get my navigation bar colored which works perfectly but, the statusbar won't change its color too. I already searched, but nothing works for me.

I hope it is possible to do this without code.


Solution

  • Here is my take on this:

    First, iOS gives only two kinds of status bar appearance - light or default. It is important to note as well that the system status bar no longer has any background color. When the API refers to UIStatusBarStyleLightContent, they mean white text on a clear background. UIStatusBarStyleDefault is black text on a clear background.

    To set status bar white throughout the app:

    UIApplication.sharedApplication().statusBarStyle = .LightContent
    

    To set status bar black throughout the app:

    UIApplication.sharedApplication().statusBarStyle = .default
    

    Second, As a side note, in your info.plist, you can use UIViewControllerBasedStatusBarAppearance to NOif you want to set the above style at app level (inside your AppDelegate). Otherwise set this property to YES and implement following method in each your view controller to set different styles based on your needs:

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }
    

    Third, To have a custom color on your status bar, you can add a UIView of desired colour just below the status bar like this:

    UIView *addStatusBar = [[UIView alloc] init];
    addStatusBar.frame = CGRectMake(0, 0, screenWidth, 20);
    addStatusBar.backgroundColor = [UIColor grayColor]]; //assign here your color 
    [self.window.rootViewController.view addSubview:addStatusBar];
    

    Fourth, importantly, UINavigationController will alter the height of its UINavigationBar to either 44 points or 64 points based on following points:

    • If the UINavigationController detects that the top of its view’s frame is visually contiguous with its UIWindow’s top, then it draws its navigation bar with a height of 64 points.
    • If its view’s top is not contiguous with the UIWindow’s top (even if off by only one point), then it draws its navigation bar in the “traditional” way with a height of 44 points.