7

I have a navigation stack that's about three views deep. I want every single destination in this stack navigation to all have exactly the same toolbar items, a "Skip" button at the top right corner. However I can't for the life of me find a way to apply that button once at the top level and have it propagate down to the children. I understand that in SwiftUI, you add modifiers like .navigationTitle to the child views and they search up the view hierarchy but is there really no way to do the opposite?

What I want would ideally look like

NavigationStack {
    RootView()
}
    .toolbar {
        ToolbarItem(placement: .topBarTrailing) {
            Button(action: {}) { Text("Skip") }
        }
    }

But with how SwiftUI works I have to apply that on every single child view. Really? There's not a better way?

8
  • Just inherit the View? Commented Feb 11, 2024 at 22:24
  • Inherit? SwiftUI views don't use inheritance. Commented Feb 11, 2024 at 22:25
  • @ElTomato are you talking about creating some shared protocol or modifier for these subviews that they all conform to or use? That's exactly the same problem because I would still have to tag each individual view with it. What I want to do is apply bar items that will never change across the entire navigation stack. Commented Feb 11, 2024 at 22:33
  • NavigationStack should be at the top of the app. there are few exceptions to this such as TabView and sheet. You can't nest them. Commented Feb 11, 2024 at 23:01
  • 2
    I'm not trying to nest them...I have a single navigation stack that I want to have the same bar item for all the views that are pushed on to that stack. I don't want to have to have each individual view define its own toolbar; I want to define it once at the top level of the navigation stack. For example, let's say I'm building a welcome flow. At the top corner of every screen in this flow is a button that says "Skip" so the user can skip the entire thing. I don't want to have to define that Skip button or attach it to child views in multiple places. Who said anything about nesting? Commented Feb 11, 2024 at 23:08

1 Answer 1

-2

Normally you would just make a View and use it in each place but in this case I think you need a ViewModifier like this:

struct MyToolBar: ViewModifier {
    func body(content: Content) -> some View {
        content
        .toolbar {
            ToolbarItem(placement: .topBarTrailing) {
                Button(action: {}) {     Text("Skip") }
            }
        }
     }

} 

Use in multiple places like:

NavigationStack {
    RootView()
    .modifier(MyToolBar())
}
Sign up to request clarification or add additional context in comments.

1 Comment

But that's still the same problem, just hiding it in a modifier. I want to apply it once at the top level, whereas with a modifier, I'd have to tag each screen in the navigation flow. I think the answer, unfortunately, is it's not possible currently in SwiftUI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.