You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NavigationBar标题颜色切换异常:返回时未恢复为白色

Fix Navigation Bar Title Color Not Reverting on Pop in Custom UINavigationController

Hey, I’ve looked over your code and spotted the issue—when popping back to your root ViewController, the title color isn’t switching back to white because your style updates are wrapped exclusively in transitionCoordinator?.animate(alongsideTransition:). This closure might not fire reliably during the pop transition, leaving the old (black) title color stuck.

The Root Cause

The willShow method does get called when returning to the root controller, but relying solely on the transition coordinator’s animation block means if the coordinator isn’t available (or the animation callback doesn’t trigger as expected), your navigation bar styles never get updated back to the root controller’s white title and cyan background.

The Fix

We’ll refactor the code to ensure style updates happen whether there’s a transition animation or not. Here’s the adjusted version of your navigationController(_:willShow:animated:) method:

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    let isSettingsContainer = viewController is ViewController
    let backgroundColor = isSettingsContainer ? UIColor.cyan : UIColor.white
    let titleColor = isSettingsContainer ? UIColor.white : UIColor.black
    let image = isSettingsContainer ? UIImage() : nil
    
    navigationController.navigationBar.shadowImage = image
    navigationController.navigationBar.setBackgroundImage(image, for: .default)
    
    // Extract style updates into a reusable closure
    let updateNavigationBarStyle = {
        navigationController.navigationBar.tintColor = titleColor
        navigationController.navigationBar.barTintColor = backgroundColor
        navigationController.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : titleColor]
    }
    
    // Apply styles with animation if possible, otherwise apply immediately
    if let coordinator = navigationController.transitionCoordinator, animated {
        coordinator.animate(alongsideTransition: { _ in
            updateNavigationBarStyle()
        })
    } else {
        updateNavigationBarStyle()
    }
    
    // Trigger status bar style update to match the current controller
    setNeedsStatusBarAppearanceUpdate()
}

What Changed?

  • Reusable Style Closure: We moved all the navigation bar style logic into a closure so we can reuse it across both animated and non-animated scenarios.
  • Fallback for No Coordinator: If there’s no transition coordinator (or the transition isn’t animated), we apply the styles directly instead of relying on the animation block. This ensures the root controller’s styles are set when popping back.
  • Status Bar Update: Added setNeedsStatusBarAppearanceUpdate() to make sure the status bar style switches back correctly alongside the navigation bar changes.

This should fix the issue—now when you pop back to the root ViewController, the title color will revert to white as expected, along with the cyan navigation bar background.

内容的提问来源于stack exchange,提问作者Sonius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:13:56