Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toolbar: icon from one view A gets added to view B #58

Closed
alelordelo opened this issue Jun 15, 2021 · 6 comments
Closed

Toolbar: icon from one view A gets added to view B #58

alelordelo opened this issue Jun 15, 2021 · 6 comments

Comments

@alelordelo
Copy link

alelordelo commented Jun 15, 2021

When used on MacOS, on PushView the toolbar icon from one view A gets added to view B.

Did anyone else experience this?

@alelordelo alelordelo changed the title NavigationBar: icon from one view A gets added to view B toolbar: icon from one view A gets added to view B Jun 15, 2021
@alelordelo alelordelo changed the title toolbar: icon from one view A gets added to view B Toolbar: icon from one view A gets added to view B Jun 15, 2021
@alelordelo
Copy link
Author

Hi @mattevigo, I am trying to solve this but still did not find a way around... Did you also experience this?

@matteopuc
Copy link
Owner

Hi @alelordelo, would you be able to provide us with a code snippet to help you debug the issue? Maybe the .toolbar modifier is attached to the wrong view, but it's just a guess. Thanks.

@alelordelo
Copy link
Author

Hi @matteopuc , there you go!

Note that the view B gets toolbar icons also from view A.

import SwiftUI
import NavigationStack



struct SidebarView: View {
    var body: some View {
        NavigationView {
            
            List {

                NavigationLink(destination: ViewA()) {
                        Label("Message", systemImage: "message")
                    }
            }
            .listStyle(SidebarListStyle())
            .navigationTitle("Explore")
            .frame(minWidth: 150, idealWidth: 250, maxWidth: 300)
            .toolbar{

            }
            //view A
            ViewA()
        }
    }
}

struct ViewA: View {
    
    
    var body: some View {
        
        NavigationStackView {

        VStack {
            
            //PushView(destination: FabricDetailPublicHStack(post: post)) {

                PushView(destination: ViewB()) {

            Text("A -> B Push")
            }
        }
        }
        .navigationTitle("A")
        
        .toolbar {

            ToolbarItem(placement: .status) {
       
            Image(systemName: "a.circle.fill")
   
            }

 
        }
    }
}

struct ViewB: View {
    var body: some View {
        
        VStack(alignment: .leading) {

            PopView {
                Image(systemName: "chevron.backward")
            }
          
            Text("B")
        }
        
        .navigationTitle("B")
        
        .toolbar {

            ToolbarItem(placement: .confirmationAction) {
       
            Image(systemName: "b.circle.fill")
   
            }

 
        }
        
    }
}

@alelordelo
Copy link
Author

Screenshot 2021-07-02 at 19 28 08

Screenshot 2021-07-02 at 19 28 16

@matteopuc
Copy link
Owner

Hi @alelordelo, thanks for the code snippet. Here is the solution: you have to move the

.toolbar {
    ToolbarItem(placement: .status) {
        Image(systemName: "a.circle.fill")
    }
}

of the ViewA inside the NavigationStackView. When you use the NavigationStackView to navigate you are basically changing it's content and not the NavigationStackView itself. If you attach a toolbar item to the NavigationStackView you'll always see that item (because the navigation stack view actually never disappears). Here is the complete solution:

import SwiftUI
import NavigationStack


struct SidebarView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: ViewA()) {
                    Label("Message", systemImage: "message")
                }
            }
            .listStyle(SidebarListStyle())
            .navigationTitle("Explore")
            .frame(minWidth: 150, idealWidth: 250, maxWidth: 300)
            ViewA()
        }
    }
}

struct ViewA: View {
    var body: some View {
        NavigationStackView {
            VStack {
                PushView(destination: ViewB()) {
                    Text("A -> B Push")
                }
                .toolbar {
                    ToolbarItem(placement: .status) {
                        Image(systemName: "a.circle.fill")
                    }
                }
            }
        }
        .navigationTitle("A")
    }
}

struct ViewB: View {
    var body: some View {
        VStack(alignment: .leading) {
            PopView {
                Image(systemName: "chevron.backward")
            }
            Text("B")
        }
        .navigationTitle("B")
        .toolbar {
            ToolbarItem(placement: .confirmationAction) {
                Image(systemName: "b.circle.fill")
            }
        }
    }
}

The result is:
Jul-05-2021 11-05-52

@alelordelo
Copy link
Author

ahhh, it was that simple! thanks a ton again Matteo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants