Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SwiftUI中的NavigationBarTitle文本颜色_Swift_Swiftui_Navigationbar - Fatal编程技术网

SwiftUI中的NavigationBarTitle文本颜色

SwiftUI中的NavigationBarTitle文本颜色,swift,swiftui,navigationbar,Swift,Swiftui,Navigationbar,我正在尝试更改导航Bartitle颜色(而不是背景)。我尝试了两种解决方案,但我的标题文本颜色只有在向下滚动时才会改变。屏幕打开时不改变 您需要为大标题添加相同的标题 .background (NavigationConfigurator { nc in nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.blue] nc.navigationBar.largeTitleTextAttr

我正在尝试更改导航Bartitle颜色(而不是背景)。我尝试了两种解决方案,但我的标题文本颜色只有在向下滚动时才会改变。屏幕打开时不改变


您需要为大标题添加相同的标题

.background (NavigationConfigurator { nc in
    
    nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.blue]
    nc.navigationBar.largeTitleTextAttributes = [.foregroundColor : UIColor.blue]
    
})
另一种方法是使用外观而不是配置器,但应该在视图的
init
中执行(创建
NavigationView
之前),如


您还可以使用更完整的方法:

    init() {
        let coloredAppearance = UINavigationBarAppearance()

            // for inline displayMode
            coloredAppearance.titleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.blue]
            
            // for automatic and large displayMode
            coloredAppearance.largeTitleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.black]
            
            // or
            
            // for inline displayMode
    //        coloredAppearance.titleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.blue]
            // for automatic and large displayMode
    //        coloredAppearance.largeTitleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.black]

            // to make everything work normally
            UINavigationBar.appearance().standardAppearance = coloredAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
    }
    var body: some View {
        NavigationView {
            Text("Screnn")
                .navigationBarTitle("Title", displayMode: .inline)
        }
        // Do not forget this
        // that means only show one view at a time no matter what device I'm working
        .navigationViewStyle(StackNavigationViewStyle())
    }
结果如下:


谢谢,它成功了。我不知道largetitle代表不同的文本。
init() {
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
    UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
}
    init() {
        let coloredAppearance = UINavigationBarAppearance()

            // for inline displayMode
            coloredAppearance.titleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.blue]
            
            // for automatic and large displayMode
            coloredAppearance.largeTitleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.black]
            
            // or
            
            // for inline displayMode
    //        coloredAppearance.titleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.blue]
            // for automatic and large displayMode
    //        coloredAppearance.largeTitleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.black]

            // to make everything work normally
            UINavigationBar.appearance().standardAppearance = coloredAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
    }
    var body: some View {
        NavigationView {
            Text("Screnn")
                .navigationBarTitle("Title", displayMode: .inline)
        }
        // Do not forget this
        // that means only show one view at a time no matter what device I'm working
        .navigationViewStyle(StackNavigationViewStyle())
    }