Ask Your Question
4

How can the color of the navigation bar title be changed in SwiftUI?

asked 2021-07-05 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-07-22 01:00:00 +0000

pufferfish gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-05 11:00:00 +0000

Seen: 11 times

Last updated: Jul 22 '22