Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)]
    }
like image 958
Ankit Srivastava Avatar asked Nov 26 '25 22:11

Ankit Srivastava


2 Answers

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.

like image 189
Sahil Kapoor Avatar answered Nov 28 '25 12:11

Sahil Kapoor


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)]

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!