所以在我的ContentView.swift中,我有以下代码:
import SwiftUI
extension UIScreen{
static let screenWidth = UIScreen.main.bounds.size.width
static let screenHeight = UIScreen.main.bounds.size.height
static let screenSize = UIScreen.main.bounds.size
}
struct ContentView: View {
@State var Direction: Int = 0
@State var ButtonsShowing: Bool = true
@State var View: Int = 0
func MoveLeft() {
if ButtonsShowing == true {
ButtonsShowing = false
}
}
func MoveRight() {
if ButtonsShowing == true {
ButtonsShowing = false
}
Direction = 1
}
var body: some View {
ZStack {
Image("Space").resizable()
.frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight + 50)
.edgesIgnoringSafeArea(.all)
VStack {
HStack {
Button(action: MoveLeft) {
if ButtonsShowing == true {
NavigationLink(destination: Playing()) {
Image("LeftClick")
.frame(width: UIScreen.screenWidth / 2, height: UIScreen.screenHeight)
}
}
}
Spacer()
Button(action: MoveRight) {
if ButtonsShowing == true {
Image("RightClick")
.frame(width: UIScreen.screenWidth / 2, height: UIScreen.screenHeight)
}
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
我得到了一个预览: Preview of button with NavigationLink
如果您朝我的LeftClick按钮看,我希望它可以将我带到另一个视图,但不知道如何。我有一个NavigationLink,目标是其他视图(Playing.swift,如果需要知道的话,我可以使用Playing()调用它)
很明显我在做错事,[[希望]]可以肯定是从按钮而不是从其他视图发出的:
NavigationLink(destination: Playing()) {
Image("LeftClick")
.frame(width: UIScreen.screenWidth / 2, height: UIScreen.screenHeight)
}
谢谢。
答案 0 :(得分:0)
在您的ZStack之前添加一个NavigationView,如果没有NavigationView,NavigationLink将无法工作。
var body: some View {
NavigationView {
ZStack {
...
}
}
}