Style a navigation bar in SwiftUI - changing its background color, text color, as well as styling the status bar. The end result looks like this:
Check out this recipe for in-depth description of the component and its code. Check out SwiftUIRecipes.com for more SwiftUI recipes!
Changing navigation bar background and text color:
var body: some View {
NavigationView {
VStack {
Text("Hello World!")
}.navigationBarColor(.blue, textColor: .white) // HERE
}
}
Changing status bar style - in your SceneDelegate.swift, replace UIHostingController
with StyledHostingController
:
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = StyledHostingController(statusBarStyle: .lightContent, rootView: contentView) // HERE
self.window = window
window.makeKeyAndVisible()
}
This component is distrubuted as a Swift package.