0

So in my ContentView.swift, I have this code:

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

I get a preview of this: Preview of button with NavigationLink

If you look towards my LeftClick button, I want it to take me to a different view, but don't know how. I have a NavigationLink in it, with my destination being the different view (Playing.swift, if needed to know that, I call it using Playing())

Obviously I'm doing something wrong, and I [[hopefully]] know for sure that it's from the button, not from the other view:

NavigationLink(destination: Playing()) {
    Image("LeftClick")
        .frame(width: UIScreen.screenWidth / 2, height: UIScreen.screenHeight)
}

Thanks in advance.

BotaGuy
  • 151
  • 1
  • 6

1 Answers1

0

Add a NavigationView before your ZStack, NavigationLink won't work without a NavigationView.

var body: some View {
  NavigationView {
    ZStack {
    ...
    }
  }
}
Watermamal
  • 357
  • 3
  • 12