To change the color of the navigation bar title in SwiftUI, you can use the navigationBarTitle
modifier and set the foregroundColor to the desired color.
For example:
struct ContentView: View {
var body: some View {
NavigationView {
Text("Hello, World!")
.navigationBarTitle("My Title")
.navigationBarTitleColor(.red) // Set the title color to red
}
}
}
extension View {
func navigationBarTitleColor(_ color: Color) -> some View {
return self.modifier(NavigationBarTitleColor(color: color))
}
}
struct NavigationBarTitleColor: ViewModifier {
var color: Color
func body(content: Content) -> some View {
content
.foregroundColor(color)
}
}
In this example, we've created an extension on View
to add a custom modifier called navigationBarTitleColor
that takes a Color
parameter. This modifier simply applies the foregroundColor
to the navigation bar title.
You can see this in action in the body
of ContentView
, where the navigationBarTitle
is set to "My Title" and the navigationBarTitleColor
is set to .red
.
Asked: 2021-07-05 11:00:00 +0000
Seen: 11 times
Last updated: Jul 22 '22