UINavigationB的UIAppearance在与自定义视图控制器一起使用时不起作用......?

时间:2016-04-28 10:53:17

标签: ios iphone swift uinavigationbar uiappearance

我有一个设置UINavigationBar外观的方法。 FlightSearchViewController是UIViewController的子类,但导航栏未按预期更新。如果我用UIViewController代替FlightSearchViewController,那么每件事都可以正常工作。

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

2 个答案:

答案 0 :(得分:2)

您的导航栏未包含在FlightSearchViewController中,而是位于视图控制器层次结构中的导航栏上方。 ' appearanceWhenContainedInInstancesOfClasses'表示viewController中包含的UINavigationBar将更新。但是,因为导航栏包含在UINavigationController中,所以情况并非如此。

如果您尝试

let navBarAppearance = UINavigationBar.appearanceWhenContainedInInstancesOfClasses([UIViewController.self]),它会奏效。但是,您将在每个视图控制器上看到更改。

答案 1 :(得分:1)

您可以尝试替换:

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

使用:

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