Swiftui 更改导航标题文本颜色快捷界面

Swiftui 更改导航标题文本颜色快捷界面,swiftui,Swiftui,我已经查看并尝试了各种不同的组合,但我不知道如何更改视图导航栏标题文本的颜色。这是我的代码,我也尝试使用我已经添加的自定义颜色,并从我的资产文件夹中使用。我知道这个问题已经被问了很多次了,但实际上我所尝试的一切都不起作用。我在Xcode 11.6 beta1 btw上 import SwiftUI import Firebase struct MyProfileView: View { let navBarAppearance = UINavigationBar.appearanc

我已经查看并尝试了各种不同的组合,但我不知道如何更改视图导航栏标题文本的颜色。这是我的代码,我也尝试使用我已经添加的自定义颜色,并从我的资产文件夹中使用。我知道这个问题已经被问了很多次了,但实际上我所尝试的一切都不起作用。我在Xcode 11.6 beta1 btw上

import SwiftUI
import Firebase

struct MyProfileView: View {

     let navBarAppearance = UINavigationBar.appearance()


    var body: some View {

        NavigationView {

            // Design the View Here
            VStack {

                //Profile Picture + Info
                VStack(alignment: .leading) {

                        //Profile Picture + Info
                        HStack(alignment: .bottom, spacing: 15) {

                        // Profile Picture
                                    Image("PROFILEPICTURE")
                                        .clipShape(Rectangle())
                                        .cornerRadius(100)
                                        .frame(width: 90, height: 90)
                                        .shadow(color: Color.black.opacity(0.3), radius: 1, x: 1, y: 1)

                        //Username + Zone
                            VStack(alignment: .leading, spacing: 3) {

                                    Text("Gardn.")
                                        .font(.system(size: 20, weight: .semibold))
                                        .foregroundColor(Color("ShipsOfficer"))

                                Text("Zone 9, Gold Base")
                                    .font(.system(size: 14, weight: .light))
                                    .foregroundColor(Color("ShipsOfficer").opacity(0.4))
                            }



                    Spacer()
                        }

                }

                .padding(.leading,30)

                // Deco line under info
                Rectangle()
                    .frame(width: 30, height: 2)
                    .foregroundColor(Color("ShipsOfficer").opacity(0.1)).cornerRadius(100)
                    .padding(.top,10)

                Spacer()

            }


            .navigationBarHidden(false)
            .navigationBarTitle(Text("My Profile"))

            .navigationBarItems(trailing:

            // Navigation Button
            NavigationLink(destination: SettingsView()) {

                Image(systemName: "slider.horizontal.3")
                    .frame(width: 25, height: 25)
                    .padding()
                    .font(.title)
                    .foregroundColor(Color("Freshness"))
                }

             )

        }

    }
    func customNavBarTitle() {

           navBarAppearance.largeTitleTextAttributes = [
               .foregroundColor : UIColor.Color("ShipsOfficer),
           ]

       }

}
您可以使用init()完成此操作

完整代码

struct YourView: View {

    init() {
        //Use this if NavigationBarTitle is with Large Font
        UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.red]

        //Use this if NavigationBarTitle is with displayMode = .inline
        UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.red]
    }

    var body: some View {

        NavigationView {
            List{
                   Text("1")
                }
            }
            .navigationBarTitle("TEST")
            
        }
    }
}

这在Xcode版本12.3(12C33)中进行了测试,其中“草莓”是assets文件夹中的cutom颜色:

struct ContentView: View {
    init() {

        UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor(named: "Strawberry") ?? .black]
        UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor(named: "Strawberry") ?? .black]
        }
    
    var body: some View {
        
        NavigationView {
            List {
                Text("Apples")
                Text("Berries")
                Text("Cookies")
                Text("Donuts")
            }
            .navigationBarTitle("My Title")
        }
    }
}


在显示NavigationBar之前应该更改外观,所以可以将其放入
init
这很好,但我不能使用自定义颜色。至少我不知道该怎么做。init(){//如果NavigationBarTitle使用大字体UINavigationBar.appearance().LarGetTitletExtAttributes=[.foregroundColor:(Color(“Shipsoficer”))]}是否有问题?
struct ContentView: View {
    init() {

        UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor(named: "Strawberry") ?? .black]
        UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor(named: "Strawberry") ?? .black]
        }
    
    var body: some View {
        
        NavigationView {
            List {
                Text("Apples")
                Text("Berries")
                Text("Cookies")
                Text("Donuts")
            }
            .navigationBarTitle("My Title")
        }
    }
}