Since SwiftUI does not expose navigation styling directly, you'll need a workaround using UINavigationController.
For first declare a struct that ability you to change navigation styling:
Code Block swiftstruct NavigationConfigurator: UIViewControllerRepresentable { |
var configure: (UINavigationController) -> Void = { _ in } |
|
func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfigurator>) -> UIViewController { |
UIViewController() |
} |
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfigurator>) { |
if let nc = uiViewController.navigationController { |
self.configure(nc) |
} |
} |
|
} |
Then, you can call this like:
Code Block swiftstruct ContentView: View { |
var body: some View { |
NavigationView { |
ScrollView { |
Text("Hello world!") |
} |
.navigationBarTitle("Try it!", displayMode: .inline) |
.background(NavigationConfigurator { nc in |
nc.navigationBar.barTintColor = .blue |
nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.white] |
}) |
} |
.navigationViewStyle(StackNavigationViewStyle()) |
} |
} |
Reference:
https://stackoverflow.com/a/58427754/9503097