NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

how to change view in swiftui?

Forums > SwiftUI

Hi, I state that I am new in swiftui, I have to display a new view by clicking on a button, this to do so I use the code below but when clicking on the button nothing is displayed:

 VStack {

                Image(systemName: "camera")
                    .font(.system(size: 26))
                    .foregroundColor(.white)
                Circle()
                    .frame(width: 6, height: 6)
                    .foregroundColor(.white)
                    .opacity(0)
            }.onTapGesture {
                print("hai fatto tap su camera")
                NavigationView {
                    NavigationLink(destination: CameraView()) {
                        Text("Push new screen")
                    }
                }

            }

1      

Hi, I would put a condition to show that navigation link, something like this:

struct ContentView: View {
    @State private var navigationIsShowing = false
    var body: some View {
        VStack {
            VStack {
                Image(systemName: "camera")
                    .font(.system(size: 26))
                    .foregroundColor(.white)
                Circle()
                    .frame(width: 6, height: 6)
                    .foregroundColor(.white)
                    .opacity(0)
            }.onTapGesture {
                print("hai fatto tap su camera")
                navigationIsShowing = true
        }
        if navigationIsShowing {
            NavigationView {
                NavigationLink(destination: Text("Camera View")) {
                    Text("Push new screen")
                }
            }
        }

        }
    }
}

So the if showing is a boolean and the onTapGesture part flips the boolean and shows the view. If you want to push the view directly when you press the camera icon I would use a sheet. Hope it helps.

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.