更改导航标题文本颜色 SwiftUI

Poo*_*lis 3 swiftui

我已经查看并尝试了所有不同的组合,但我无法弄清楚如何更改视图导航栏标题的文本颜色。这是我的代码,我也在尝试使用我已经从我的资产文件夹中添加和使用的自定义颜色。我知道这个问题已被问过多次,但实际上我尝试的所有方法都不起作用。顺便说一句,我在 Xcode 11.6 beta1 上。

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),
           ]

       }

}
Run Code Online (Sandbox Code Playgroud)

Dc7*_*Dc7 11

你可以用 init()

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]
}
Run Code Online (Sandbox Code Playgroud)

完整代码

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")
            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ril*_*eux 5

这是在 Xcode 版本 12.3 (12C33) 中测试的,其中“草莓”是资产文件夹中的自定义颜色:

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")
        }
    }
}

Run Code Online (Sandbox Code Playgroud)