I have text button, and I want to use navigation link in this project, but it is not work when I click the button? Any idea?
@State var isLink = false
var body: some View {
GeometryReader { geometry in
ZStack {
NavigationLink(destination: SecondView(), isActive:
$isLink) {
Button() {
self.isLink = true
} label: {
Text("Button")
.padding()
.frame(width: 250, height: 50,
alignment: .center)
.foregroundColor(Color.white)
.background(Color("color"))
.clipShape(Capsule())
}}}}
You need to include in a NavigationView:
struct ContentView: View {
@State var isLink = false
var body: some View {
NavigationView { // <<-- Need this
GeometryReader { geometry in
VStack {
NavigationLink(destination: SecondView(), isActive:
$isLink) {
Button(action: {
self.isLink = true
}) {
Text("Button")
.padding()
.frame(width: 250, height: 50,
alignment: .center)
.foregroundColor(Color.blue) // Changed to blue to get it visible
.background(Color("color"))
.clipShape(Capsule())
}
}
}
}
}
}
}