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

Swift5下导航栏标题颜色未变白问题求助(附AppDelegate代码)

问题原因与解决方案

你遇到的问题大概率是iOS 13+ 引入的新导航栏外观系统导致旧设置方式失效。在iOS 13之后,苹果推荐使用UINavigationBarAppearance类来管理导航栏的各种状态外观(标准状态、滚动到顶部状态、紧凑布局状态等),直接修改UINavigationBar.appearance()的titleTextAttributes会被默认的外观配置覆盖,所以你的白色标题设置不生效。

修复步骤

修改你的AppDelegate代码,改用新的UINavigationBarAppearance API来配置:

var navigationBarAppearance = UINavigationBar.appearance() 
var tabBarAppearance = UITabBar.appearance() 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 
    FirebaseApp.configure() 

    // 针对iOS 13+配置导航栏外观
    let navBarAppearance = UINavigationBarAppearance()
    // 设置标题文字颜色为白色
    navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    // 可选:设置导航栏背景色(根据你的UI需求调整,比如深色背景让白色标题更显眼)
    navBarAppearance.backgroundColor = UIColor.systemBlue // 也可以用自定义颜色
    // 将外观应用到导航栏的所有状态
    navigationBarAppearance.standardAppearance = navBarAppearance
    navigationBarAppearance.scrollEdgeAppearance = navBarAppearance
    navigationBarAppearance.compactAppearance = navBarAppearance // 适配紧凑布局场景

    navigationBarAppearance.tintColor = UIColor.white 
    tabBarAppearance.tintColor = UIColor.white 

    print(Realm.Configuration.defaultConfiguration.fileURL!) 
    do{ 
        let _ = try Realm() 
    }catch{ 
        print("Error initializing new realm, \(error)") 
    } 
    return true 
}

额外说明

  1. 兼容旧版本:如果你的App需要支持iOS 12及以下版本,可以添加版本判断来兼容:
if #available(iOS 13.0, *) {
    // 上面的新API配置代码
} else {
    // 旧版本的设置方式
    navigationBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
}
  1. 检查局部配置:如果某个页面的导航栏标题还是黑色,要确认该页面的导航控制器没有单独设置标题颜色——局部设置会覆盖全局的外观配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 09:32:38