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

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")
                    }
                }

            }

3      

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.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.