UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to change from NavigationLink or Model Button on call?

Forums > SwiftUI

I have a View that can be called via a NavigationLink or Modal Sheet. My Problem is that no toolbar Item show in the model view else you wrap it in a NavigationView, however when called from NavigationLink creates an extra title spaces

struct ViewA: View {
    @State private var isPresented = false

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: ViewB()) {
                    Text("Navigation Link")
                }
            }
            .navigationTitle("Main")
            .toolbar {
                ToolbarItem {
                    Button("Modal") {
                        isPresented = true
                    }
                }
            }
            .sheet(isPresented: $isPresented) {
                ViewB() // change this when using ViewC()
            }
        }
    }
}

struct ViewB: View {
    @Environment(\.presentationMode) var presentationMode
/// Uncomment `NavigationView` to and call from toolbar button
    var body: some View {
//        NavigationView {
            VStack {
                Text("View B")
            }
            .toolbar {
                ToolbarItem {
                    Button("Close") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
            .navigationBarBackButtonHidden(true)
//        }
    }
}

The only hack that I have come up with is to have the Model in another view with NavigationLink and call that from the .sheet(: )

struct ViewC: View {
    var body: some View {
        NavigationView {
            ViewB()
        }
    }
}

I have made this simple but my project has properties (which some are @Binding) passed to and these have then got passed though ViewC

3      

hi Nigel,

had you tried the following?

.sheet(isPresented: $isPresented) {
  NavigationView {
    ViewB()
  }
}

i think that should work (i've used it a few times). it seems your ViewC idea does this, but i'll wait to see more from you about other properties you want to incorporate.

hope that helps,

DMG

3      

@Mary  

Hi,

You can hide the navigation bar on the sheet you're linking to using .navigationBarHidden(true).

I think that should work.

Hope it helps!

Mary

3      

Thank you DMG that was just what i was trying to do, I could see the woods for the trees

Thank you Mary for your suggestion but I did think the same and when I tried it remove the wrong navigation bar!

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.