NavigationView dynamic background color in SwiftUI
Problem
As you may noticed, customizing navigation bar using only SwiftUI standard NavigationView is not possible in the first release of SwiftUI (as of Xcode 11), as no API is provided to change the background color. More over you as you don’t have access to NavigationBarController is really hard to achieve any customization.
In this article, I will show you how to compose your view to get the intended behavior.
Solution
Where are going to create custom modifier so we can change the color of NavBar on every view. The usage will be then as follow
struct SomeView: View { | |
var body: some View { | |
NavigationView { | |
Text("Hello World") | |
} | |
.navigationBarColor(.green) // This is how you will use it | |
} | |
} | |
} |
An here is implementation. Fist of all we will create our modifier:
And then we will extend the view so we can use it anywhere in SwiftUI scope.
iOS Elements
You can find also the whole implementation in my UI kit — Download below.
Use discount code MOLCIK to get iOS Elements for $29.70 (70% off)