Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change navigation title text color SwiftUI

Tags:

swiftui

I have looked and tried every different combo and I can't figure out how to change the color of the text for my view's navigation bar title. Here is my code, also I'm trying to use a custom color that I have already added and used from my assets folder. I know this question has been asked multiple times but literally everything I try doesn't work. I'm on 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),
           ]

       }

}
like image 959
PoonDopolis Avatar asked Dec 08 '22 10:12

PoonDopolis


2 Answers

You can do it with 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]
}

Full Code

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")
            
        }
    }
}
like image 199
Dc7 Avatar answered Dec 10 '22 00:12

Dc7


This is tested in Xcode Version 12.3 (12C33), where "Strawberry" is a cutom color in the assets folder :

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

like image 22
Rillieux Avatar answered Dec 09 '22 23:12

Rillieux