简体   繁体   English

当导航栏的颜色在不同的View Controller中变化时,如何更改后退按钮的颜色?

[英]How to change the colour of the back button when the colour of the navigation bar changes across different View Controllers?

I have different bar tint colours for different view controllers. 我为不同的视图控制器使用不同的条形颜色。 Some View Controllers' bar tint colour requires the title text colour to be white and other View Controllers' bar tint colour requires it to be black. 一些View Controller的条形颜色要求标题文本颜色为白色,而其他View Controller的条形颜色要求颜色为黑色。 Now the code (called in the viewWillAppear method) I'm using for the view controllers with the white navigation bars is: 现在,我用于带有白色导航栏的视图控制器的代码(在viewWillAppear方法中调用)是:

    self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!,  NSForegroundColorAttributeName: UIColor.blackColor()]

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)

The code (called in the viewWillAppear method) I am using for the darker navigation bars is: 我用于较深的导航栏的代码(在viewWillAppear方法中调用)是:

    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!,  NSForegroundColorAttributeName: UIColor.whiteColor()]

     UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: UIControlState.Normal)

Now the problem is, my first two view controllers have white navigation bars, and my third view controller has a darker navigation bar. 现在的问题是,我的前两个视图控制器具有白色导航栏,而我的第三个视图控制器具有较暗的导航栏。

Moving from the first to the second view controller, the back button appears normally, and is black. 从第一个视图控制器移动到第二个视图控制器,后退按钮通常显示为黑色。 Moving from the second to third view controller the back button appears normally once again, and is white. 从第二个视图控制器移动到第三个视图控制器,后退按钮再次正常显示,并且为白色。

But after I press the back button on the third view controller and come to the second view controller, the back button remains white and is illegible (which I don't want). 但是,当我按下第三个视图控制器上的“后退”按钮并进入第二个视图控制器后,“后退”按钮保持白色并且难以辨认(我不想要)。 It turns black only when I tap on it; 只有当我点击它时它才会变成黑色。 you can't see it before tapping on the top left corner. 在点击左上角之前看不到它。 How do I make it black? 我如何使它变黑?

I've tried calling these methods in the viewDidLoad, the viewDidAppear and the viewWillAppear functions, but it just doesn't solve the problem. 我试过在viewDidLoad,viewDidAppear和viewWillAppear函数中调用这些方法,但这并不能解决问题。

I need help resolving this irritating glitch. 我需要帮助解决这种令人讨厌的故障。 Thanks. 谢谢。

Just set all the things directly without appearance proxy. 无需外观代理即可直接设置所有内容。 Appearance proxy sets values for the whole app and it should be configured on early stages of app running. 外观代理会为整个应用设置值,并且应在应用运行的早期阶段对其进行配置。 The best place for this is application:didFinishLaunchingWithOptions: of AppDelegate class. 最好的地方是AppDelegate类的application:didFinishLaunchingWithOptions: AppDelegate You could do it like this: 您可以这样做:

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

func configureAppearance() {

    // Configure for all UIBarButtonItems

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)


    // Configure for your view controller(s) where black button has to be

    UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([YourViewControllerWithBlackColorBtn.self]).setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM