SwiftUI NavigationView转换不工作



我做了一个项目来测试导航过渡。

  • 这个有效
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)
}
}
}

当点击第一个屏幕的按钮时,它会进入第二视图。

但是下面的代码不工作,我不知道为什么!!


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

}

我认为两者逻辑相同。

当点击或触摸按钮时,改变@State bool值,它会导致过渡

但是下面的代码不能工作,尽管布尔值改变了。

必须加上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()
}
}
} } }

相关内容

  • 没有找到相关文章

最新更新