Ankit Srivastava
Ankit Srivastava

Reputation: 12405

UIAppearance for UINavigationBar not working when used with custom view controller...?

I have a method which sets the appearance of UINavigationBar. FlightSearchViewController is subclassed from UIViewController but the nav bar is not updated as expected. If I write UIViewController in place of FlightSearchViewController every thing works fine.

private class func setupNavigationBarAppearance() {
        UINavigationBar.appearance().barStyle = .Black
        UINavigationBar.appearance().translucent = false
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor(), NSFontAttributeName: UIFont.ixiRegularFontOfSize(17)]
        UINavigationBar.appearance().tintColor = UIColor.clearColor()
        UINavigationBar.appearance().barTintColor = Color.navBarThemeColor

        var navBarAppearanceControllers = [AnyObject.Type]()
        navBarAppearanceControllers.append(FlightSearchViewController.self)
        let navBarAppearance = UINavigationBar.appearanceWhenContainedInInstancesOfClasses(navBarAppearanceControllers)
        navBarAppearance.barTintColor = UIColor.clearColor()
        navBarAppearance.backgroundColor = UIColor.clearColor()
        navBarAppearance.tintColor = UIColor.clearColor()
        navBarAppearance.setBackgroundImage(UIImage(), forBarMetrics: .Default)
        navBarAppearance.shadowImage = UIImage()
        navBarAppearance.translucent = true
        navBarAppearance.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor(), NSFontAttributeName: UIFont.ixiRegularFontOfSize(17)]
    }

Upvotes: 3

Views: 3048

Answers (2)

Sahil Kapoor
Sahil Kapoor

Reputation: 11773

Your navigation bar is not contained in FlightSearchViewController, rather it is a above it in view controller hierarchy. 'appearanceWhenContainedInInstancesOfClasses' means UINavigationBar contained in your viewController will get updated. But it isn't the case since navigationBar in contained in UINavigationController.

If you try

let navBarAppearance = UINavigationBar.appearanceWhenContainedInInstancesOfClasses([UIViewController.self]), it will work. But, you will see the changes on every view controller.

Upvotes: 2

Terry
Terry

Reputation: 577

Can you try to replace:

    var navBarAppearanceControllers = [AnyObject.Type]()
    navBarAppearanceControllers.append(FlightSearchViewController.self)
    let navBarAppearance = UINavigationBar.appearanceWhenContainedInInstancesOfClasses(navBarAppearanceControllers)
    navBarAppearance.barTintColor = UIColor.clearColor()
    navBarAppearance.backgroundColor = UIColor.clearColor()
    navBarAppearance.tintColor = UIColor.clearColor()
    navBarAppearance.setBackgroundImage(UIImage(), forBarMetrics: .Default)
    navBarAppearance.shadowImage = UIImage()
    navBarAppearance.translucent = true
    navBarAppearance.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor(), NSFontAttributeName: UIFont.ixiRegularFontOfSize(17)]

With:

    UINavigationBar.appearance().barTintColor = UIColor.clearColor()
    UINavigationBar.appearance().backgroundColor = UIColor.clearColor()
    UINavigationBar.appearance().tintColor = UIColor.clearColor()
    UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().translucent = true
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor(), NSFontAttributeName: UIFont.italicSystemFontOfSize(17)]

Upvotes: 1

Related Questions