更改导航标题文本颜色SwiftUI

时间:2020-06-17 01:58:25

标签: swiftui

我已经查看并尝试了每种不同的组合,但是我不知道如何更改视图导航栏标题的文本颜色。这是我的代码,也是我尝试使用自定义颜色,该颜色已经在我的Assets文件夹中添加并使用过。我知道这个问题已经被问过多次了,但实际上我尝试的所有方法都不起作用。我正在使用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),
           ]

       }

}

2 个答案:

答案 0 :(得分:1)

您可以使用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]
}

完整代码

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

    }
}

}

答案 1 :(得分:0)

这是在 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")
        }
    }
}