How to turn the Navigation Bar in iOS 6 opaque when compiling with Xcode 5

Xcode 5 is nice… but it has habits that drive me up the wall. For example, no matter which Base SDK you compile with, your Navigation Bars will always look translucent – no matter how hard you try to avoid this in the Storyboard, and no matter which iOS Version you deploy to.

Would have never happened under Xcode 4. But I suppose we must go with the times.

All we can do is to turn it opaque in code – and if we’re not using black, at least give iOS 7 a tint of your selected colour at the same time. This method will take a UINavigationController and change its colour:

- (void)navBarColour:(UINavigationController *)navController {
    
    if ([navController.navigationBar respondsToSelector:@selector(barTintColor)]) {
        
        // we're running iOS 7
        navController.navigationBar.barTintColor = [UIColor blackColor];
        
    } else {
        
        // we're on iOS 6 and before
        navController.navigationBar.tintColor = [UIColor blackColor];
    }
}

Note that on iOS 7 your navigation bars are always translucent, I haven’t found a way to change this. All you can do is “tint” those. The above code will however bring opaqueness back into your apps when running on iOS 6.





2 thoughts on “How to turn the Navigation Bar in iOS 6 opaque when compiling with Xcode 5

  1. Minor snag:
    If you run the above code on on iOS 6 device, and you’re compiling with the iOS 6 SDK, the compiler will throw an error saying that UINavigationController does not respond to tintColor.

    A better way to determine iOS version is this:

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
       // Load resources for iOS 6.1 or earlier
    } else {
       // Load resources for iOS 7 or later
    }
    
  2. This works fine on both iOS 6.x and iOS 7.1:

    - (void)tintMyNavBar {
        
        if ([self.navigationController.navigationBar respondsToSelector:@selector(barTintColor)]) {
            self.navigationController.navigationBar.tintColor = [UIColor greenColor];
        }
    }
    

Leave a Reply