简体   繁体   English

如果点击后退按钮,导航栏标题颜色不会改变

[英]Navigation bar title color does not change if back button is tapped

Changing the color of the title in the navigation bar does not work if back button is tapped.如果点击后退按钮,则无法更改导航栏中标题的颜色。

There is a UINavigationController.有一个 UINavigationController。

Its rootViewController is VC1.它的rootViewController是VC1。 The color of the title in the navigation bar is set to red.导航栏中标题的颜色设置为红色。

VC1 pushes VC2. VC1 推动 VC2。 The color of the title in the navigation bar is set to green.导航栏中标题的颜色设置为绿色。

When VC2 is popped, the color of the title in the navigation bar is set to red.弹出VC2时,导航栏标题的颜色设置为红色。

If the back button is used to pop VC2, the title of the navigation bar does not change to red and remains green.如果使用返回按钮弹出VC2,导航栏标题不会变为红色,保持绿色。

If the swipe back gesture is used to pop VC2, the title of the navigation bar changes to red as expected.如果使用向后滑动手势弹出 VC2,导航栏的标题按预期变为红色。

The navigation controller is its own delegate.导航 controller 是它自己的代表。 Using the navigationController(_:willShow:animated) method, the color of the tile in the navigation bar is set.使用navigationController(_:willShow:animated)方法,设置导航栏中 tile 的颜色。

extension NavigationController: UINavigationControllerDelegate {
    func navigationController(
        _ navigationController: UINavigationController,
        willShow viewController: UIViewController,
        animated: Bool
        ) {
        let attributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: viewController is VC1 ? UIColor.red : UIColor.green
        ]
        navigationBar.titleTextAttributes = navigationBar.titleTextAttributes?.merging(attributes) { $1 }
    }
}

Expected- After the VC2 is popped, the color of the title in the navigation bar should change to red.预期 - 弹出 VC2 后,导航栏中标题的颜色应变为红色。

Actual- After the VC2 is popped, the color of the title in the navigation bar remains green.实际-弹出VC2后,导航栏标题颜色保持绿色。

Can anyone point out how to solve this issue, so that the color of the title in the navigation bar becomes red after VC2 is popped out?谁能指出如何解决这个问题,使VC2弹出后导航栏中标题的颜色变为红色?

Do this in ViewDidLoad()在 ViewDidLoad() 中执行此操作

let image = UIImage(named: "back")
        self.navigationController?.navigationBar.backIndicatorImage = image
        self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = image
        self.navigationItem.title = "Servicos"
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

If both of the viewController has large title enabled.如果两个 viewController 都启用了大标题。 Otherwise use titleTextAttributes .否则使用titleTextAttributes It is required that the two of your ViewControllers' UINavigationController's delegate is NavigationController要求您的 ViewControllers 的 UINavigationController 的两个delegateNavigationController

extension NavigationController: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        if let _ = viewController as? ToViewController {
            let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
            navigationController.navigationBar.largeTitleTextAttributes = textAttributes
        } else {
            let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.green]
            navigationController.navigationBar.largeTitleTextAttributes = textAttributes
        }
    }
}

This worked for me.这对我有用。 Both of my viewControllers has large title.我的两个视图控制器都有大标题。

Have you tried changing navigationBar tint color?您是否尝试过更改导航栏的色调?

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

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