[英]How to change the colour of the back button when the colour of the navigation bar changes across different View Controllers?
我为不同的视图控制器使用不同的条形颜色。 一些View Controller的条形颜色要求标题文本颜色为白色,而其他View Controller的条形颜色要求颜色为黑色。 现在,我用于带有白色导航栏的视图控制器的代码(在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)
我用于较深的导航栏的代码(在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)
现在的问题是,我的前两个视图控制器具有白色导航栏,而我的第三个视图控制器具有较暗的导航栏。
从第一个视图控制器移动到第二个视图控制器,后退按钮通常显示为黑色。 从第二个视图控制器移动到第三个视图控制器,后退按钮再次正常显示,并且为白色。
但是,当我按下第三个视图控制器上的“后退”按钮并进入第二个视图控制器后,“后退”按钮保持白色并且难以辨认(我不想要)。 只有当我点击它时它才会变成黑色。 在点击左上角之前看不到它。 我如何使它变黑?
我试过在viewDidLoad,viewDidAppear和viewWillAppear函数中调用这些方法,但这并不能解决问题。
我需要帮助解决这种令人讨厌的故障。 谢谢。
无需外观代理即可直接设置所有内容。 外观代理会为整个应用设置值,并且应在应用运行的早期阶段对其进行配置。 最好的地方是AppDelegate
类的application:didFinishLaunchingWithOptions:
AppDelegate
您可以这样做:
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.