UINavigationBar 的 UIAppearance 在与自定义视图控制器一起使用时无法正常工作...?

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

我有一个方法可以设置 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)]
    }

你可以尝试替换:

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

您的导航栏未包含在 FlightSearchViewController 中,而是在视图控制器层次结构中位于其上方。 'appearanceWhenContainedInInstancesOfClasses' 意味着 viewController 中包含的 UINavigationBar 将得到更新。但事实并非如此,因为导航栏包含在 UINavigationController 中。

如果你尝试

let navBarAppearance = UINavigationBar.appearanceWhenContainedInInstancesOfClasses([UIViewController.self]),会起作用的。但是,您会在每个视图控制器上看到更改。