【问题标题】:Change navigation bar tittle text to custom color?将导航栏标题文本更改为自定义颜色?
【发布时间】:2020-11-10 05:44:13
【问题描述】:

我正在尝试使用我的 appDelegate 中的以下代码将标题栏文本更改为自定义的淡紫色阴影

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(red: 0.9069682956, green: 0.7839415669, blue: 0.9612388015, alpha: 1)]

    return true
}

但是标题栏文本仍然显示为黑色。有没有更好的方法来改变这种情况?

【问题讨论】:

    标签: ios swift swiftui uikit


    【解决方案1】:

    可能还为时过早,尝试在ContentView 中添加init,以及为大标题添加一些(这是SwiftUI NavigationView 的默认设置),例如

    struct ContentView: View {
    
        init() {
            UINavigationBar.appearance().titleTextAttributes = 
                [NSAttributedString.Key.foregroundColor: 
                   UIColor(red: 0.9069682956, green: 0.7839415669, blue: 0.9612388015, alpha: 1)]
            UINavigationBar.appearance().largeTitleTextAttributes = 
                [NSAttributedString.Key.foregroundColor: 
                   UIColor(red: 0.9069682956, green: 0.7839415669, blue: 0.9612388015, alpha: 1)]
        }
    
        // ... other code with NavigationView here
    }
    

    使用 Xcode 12.1 / iOS 14.1 测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2023-03-19
      相关资源
      最近更新 更多