Zappel
Zappel

Reputation: 1751

UINavigationBar does not extend behind status bar

I feel like I'm completely overlooking something, since this is so basic.

In a completely bare bones setup:

window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.backgroundColor = UIColor.whiteColor()

let rootController = MainViewController()
rootNavigationController = UINavigationController(rootViewController: rootController)

window.rootViewController = rootNavigationController;
window.makeKeyAndVisible()

// Appearance
UINavigationBar.appearance().barTintColor = UIColor.DailyRate.blueColor
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().opaque = false

I get a navigation bar that does not extend behind the status, which should be default behavior.

enter image description here

Upvotes: 0

Views: 1628

Answers (3)

MLQ
MLQ

Reputation: 13511

I encountered this problem because I was removing the built-in line border at the bottom of the navigation bar, like this:

if let navigationBar = self.navigationController?.navigationBar {
    navigationBar.shadowImage = UIImage()
    navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
    navigationBar.backgroundColor = UIColor.redColor()
}

I was doing the above code inside my view controller's viewWillAppear because some of my VCs have other colors for the navigation bar and I don't want to modify the universal appearance.

The solution was to just create a 1 pt x 1 pt image with the color I want and use it instead of a new empty UIImage instance, like this:

if let navigationBar = self.navigationController?.navigationBar {
    let colorImage = UIImage.imageWithColor(self.category.color)
    navigationBar.shadowImage = colorImage
    navigationBar.setBackgroundImage(colorImage, forBarMetrics: .Default)
    navigationBar.tintColor = UIColor.whiteColor()
}

imageWithColor is a function I defined in an extension to UIImage:

class func imageWithColor(color: UIColor) -> UIImage {
    let rect = CGRectMake(0, 0, 1, 1)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()

    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillRect(context, rect)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

Upvotes: 1

Zappel
Zappel

Reputation: 1751

It turned out that it was a timing issue.

The root hierarchy was actually set up in the initializer of a separate class called UIManager. However, this class was initialized at the same time as the AppDelegate

var uiManager = UIManager()

and not in application(_, didFinishLaunchingWithOptions _) method, thus creating this weird scenario.

So all I did was

var uiManager: UIManager?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    uiManager = UIManager()

}

And now everything is back to normal.

Thanks to @govindarao-kondala for planting the right idea in my head!

Upvotes: 0

Govindarao Kondala
Govindarao Kondala

Reputation: 2862

I just tried and I got proper result. Please find my complete code. I can not find few things in your code(I don't get what you mean by DailyRate) , remaining things are same as your code.

var window: UIWindow?
    var rootNavigationController : UINavigationController?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window!.backgroundColor = UIColor.whiteColor()

    let rootController = ViewController()
    rootNavigationController = UINavigationController(rootViewController: rootController)

    window!.rootViewController = rootNavigationController;
    window!.makeKeyAndVisible()

    // Appearance
    UINavigationBar.appearance().barTintColor = UIColor.blueColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    UINavigationBar.appearance().opaque = false

    // Override point for customization after application launch.
    return true
}

And the result is in following attachment. enter image description here

Upvotes: 1

Related Questions