I am new to swift, and I am trying to place a "quick exit" button on each content page of my app, the button itself is how I want it, but I am having a hard time getting the actual navigation to work. It builds, but simply does nothing when clicked.
struct ContentMenu: View {
var body: some View {
ZStack{
VStack {
Spacer()
HStack {
Spacer()
NavigationLink(destination: ContentView2())
{
Button(action:{} ){
Image(systemName: "figure.run.circle.fill")
.resizable(resizingMode: .stretch)
.font(.largeTitle)
.frame(width: 65, height: 65)
.background(Color .red)
.clipShape(Circle())
.foregroundColor(Color .black)}
}.padding()
.shadow(radius: 5)
}
}
}
}
}
>Solution :
In SwiftUI, the NavigationLink itself is a special type of button. So you absolutely do not need to wrap a button inside a NavigationLink. Simply do:
NavigationLink(destination: ContentView2()) {
Image(systemName: "figure.run.circle.fill")
.resizable(resizingMode: .stretch)
.font(.largeTitle)
.frame(width: 65, height: 65)
.background(Color .red)
.clipShape(Circle())
.foregroundColor(Color .black)}
}.padding()
.shadow(radius: 5)
Don’t forget to wrap your view inside a NavigationStack or a NavigationView (if you plan to support iOS 15-) in order for the navigation to work.