Hyun Bin CHo
Hyun Bin CHo

Reputation: 93

SwiftUI NavigationView transition does not work

I have made a project to test Navigation Transition.

import SwiftUI

struct ContentView: View {
    
    @State var isSecondViewShowing: Bool = false
    
    
    var body: some View {
        NavigationView {
            ZStack{
                Color.black
                VStack {
                    Text("Hello, world!")
                        .font(.headline)
                        .foregroundColor(
                            isSecondViewShowing ? Color.gray : Color.white
                        )
                        .onTapGesture {
                            
                            isSecondViewShowing = true
                            print("isSecondViewShowing Touch")
                        }
                    NavigationLink(destination: SecondView(), isActive: $isSecondViewShowing) {
                        EmptyView()
                    }
                    .navigationTitle("")
                    .navigationBarHidden(true)
                    .hidden()
                }
            }
        }
    }
}


import SwiftUI

struct SecondView: View {
    var body: some View {
        ZStack {
            Color.gray
            Text("SecondView")
                .font(.headline)
                .foregroundColor(.blue)
        }
    }
}

When tap the button in 1st screen, it goes to Secondview.

But codes below does not work, and I don't know why!!


import SwiftUI

struct ContentView: View {
    
    @State var isPickerShowing: Bool = false
    @State var isEditViewShowing: Bool = false
    @State private var pickImage: UIImage?
    @State private var editImage: UIImage?
    
    var body: some View {
        NavigationView {
            Button {
                isEditViewShowing = true
                print(String(isEditViewShowing))
            } label: {
                Text("First Screen")
                    .font(.system(size: 50))
                    .foregroundColor(.green)
                    .background(Color.yellow)
            }
            NavigationLink(destination: PhotoEditView(), isActive: $isEditViewShowing) {
                EmptyView()
            }
        }
}



import SwiftUI

struct PhotoEditView: View {
    
    init() {
        print("PhotoEditView : Init")
    }
    
    
    var body: some View {
        ZStack {
            Color.blue
            Text("Second View")
                .foregroundColor(.white)
                .font(.headline)
        }
    }
    
}

I think both have same logic.

when tap or touch the button, change the @State bool value and it leads to transition

But codes below does not work although boolean value changed.

Upvotes: 1

Views: 165

Answers (1)

darshilsakhiya
darshilsakhiya

Reputation: 659

You have to add ZStack{}

struct ContentView: View {

@State var isEditViewShowing: Bool = false

var body: some View {
    NavigationView {
        ZStack { // here
            Button {
                isEditViewShowing = true
            } label: {
                Text("First Screen")
                    .font(.system(size: 50))
                    .foregroundColor(.green)
                    .background(Color.yellow)
        }
        
        NavigationLink(destination: PhotoEditView(), isActive: $isEditViewShowing) {
            EmptyView()
        }
    }
    } } }

Upvotes: 4

Related Questions