Let's say I have 10 different navigationLinks, do I have to set my custom background for every single navigationLink?
import SwiftUI
struct TryAvoidSettingBackgroundColorForEveryNavigationLink: View {
var body: some View {
NavigationStack {
VStack {
Text("Root View")
.padding()
.background(Color.green)
NavigationLink("Go to Detail", value: "Detail View")
.padding()
}
.background(Color.red) // This doesn't override default background
.navigationDestination(for: String.self) { value in
Text(value)
.padding()
// Unless i uncomment this, I get the system background
// .background(Color.red)
}
}
.background(Color.red) // This doesn't override default background
}
}
#Preview {
TryAvoidSettingBackgroundColorForEveryNavigationLink()
}
You have to add a background to each of your navigation destinations. You can try to make this less tedious by using creating your own EnvironmentKey
for the navigation background, and also your custom navigationDestination
modifier to automatically add the background for you.
Here is an example:
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
Text("Root View")
.padding()
.background(Color.green)
NavigationLink("Go to Detail", value: "Detail View")
.padding()
}
.navigationDestinationWithBackground(for: String.self) { value in
Text(value)
.padding()
}
.navigationBackground()
}
.environment(\.navigationBackground, AnyShapeStyle(.red))
}
}
struct NavigationBackgroundModifier: ViewModifier {
@Environment(\.navigationBackground) var bg
func body(content: Content) -> some View {
content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(bg)
}
}
struct NavigationBackgroundKey: EnvironmentKey {
static var defaultValue: AnyShapeStyle { AnyShapeStyle(.background) }
}
extension EnvironmentValues {
var navigationBackground: AnyShapeStyle {
get { self[NavigationBackgroundKey.self] }
set { self[NavigationBackgroundKey.self] = newValue }
}
}
extension View {
func navigationBackground() -> some View {
modifier(NavigationBackgroundModifier())
}
func navigationDestinationWithBackground<D, C>(
for data: D.Type,
@ViewBuilder destination: @escaping (D) -> C
) -> some View where D : Hashable, C : View {
navigationDestination(for: data) {
destination($0)
.navigationBackground()
}
}
}
Note the line .environment(\.navigationBackground, AnyShapeStyle(.red))
, where I set the navigation background to red. Then, you can replace all the navigationDestination
modifiers with the navigationDestinationWithBackground
modifier, which automatically adds a background.
To expand this further, you can create your own version of the other overloads of navigationDestination
too, like navigationDestination(isPresented:destination:)
and so on.
An alternative way is to swizzle the methods of UINavigationController
(setViewControllers
and pushViewController
), and set the pushed views' background color in the swizzled methods. This obviously will affect all the navigation stacks in your app, and you cannot easily change the background later on. You also cannot use any ShapeStyle
- only UIColor
s.
Note that you cannot set the delegate
of the UINavigationController
that backs the navigation stack, because SwiftUI needs to use the delegate for its own purposes. Setting it to your own object will cause SwiftUI to not behave correctly.