How to change navigation title color in swiftUI
Hi, There
I am learning SwiftUI, I want change navigation Title Color. I have set navigation Title using .navigationTitle("Parent Login")
I have tried to color of navigation title using below code.
struct ContentView: View {
var body: some View {
NavigationView {
Text("Hello, World!")
.navigationBarTitle("My Title", displayMode: .inline)
.navigationBarItems(trailing: Text("Trailing"))
.navigationBarTitleColor(.red) // Set the navigation title color here
}
}
}
extension View {
func navigationBarTitleColor(_ color: Color) -> some View {
return self.modifier(NavigationBarTitleColorModifier(color: color))
}
}
struct NavigationBarTitleColorModifier: ViewModifier {
var color: Color
func body(content: Content) -> some View {
content
.onAppear() {
let coloredAppearance = UINavigationBarAppearance()
coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor(color)]
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor(color)]
UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
}
}
}
I have checked apple's document but did not get proper demo or answer to change color.
Please comment if anyone has idea about it
Apple also deprecated .navigationBarTitle(<#T##title: Text##Text#>) method which takes Text styled view so we can change navigation title color.
MacBook Pro 16″, macOS 11.2