NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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

            }

   

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.

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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

Reply to this topic…

You need to create an account or log in to reply.

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.