SwiftUI: how to keep single toolbar or ToolbarItem on the next view in NavigationView stack?
Asked Answered
W

0

6

Each toolbar applied to the NavigationView destination is in fact separate toolbar, and one toolbar is changing to another with opacity transition.

How to keep one single toolbar or only ToolbarItem on the next destination, so It could be animated for example, let's say trailing ToolbarItem was containing 2 buttons in HStack, and I want to insert another one with animation like I'd do that in one single View?

Want to take a full control over the ToolbarItem like it was a single item.

struct Temp_Preview: PreviewProvider {
    static var previews: some View {
        return NavigationView {
            NavigationLink {
                Text("TARGE TPAGE")
                    .toolbar {
                        ToolbarItem(placement: .navigationBarTrailing) {
                            ToolBarView(showExtraButton: true)
                        }
                    }
            } label: {
                Text("Next page")
                    .toolbar {
                        ToolbarItem(placement: .navigationBarTrailing) {
                            ToolBarView(showExtraButton: false)
                        }
                    }
            }

        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct ToolBarView: View {
    let showExtraButton: Bool
    var body: some View {
        HStack {
            let buttonsCount = showExtraButton ? 3 : 2
            ForEach(0..<buttonsCount, id: \.self) { index in
                Button(action: {}) {
                    Color.blue
                        .cornerRadius(5)
                        .frame(width: 80 - CGFloat(20 * index), height: 20)
                }
            }
        }
    }
}

enter image description here

Wardieu answered 7/4, 2022 at 13:42 Comment(2)
See https://mcmap.net/q/1918734/-persistent-toolbar-in-swiftuiAbramson
@Abramson What is it?Wardieu

© 2022 - 2025 — McMap. All rights reserved.