当我将VStack放入列表中并且VStack的一个子视图中的一个是NavigationLink时,整个VStack区域变得可轻按以触发转换,而不仅仅是SubView。
这是我的问题的简化示例:
struct Test: View {
var body: some View {
NavigationView {
List {
VStack { // Entire VStack is tappable.
Rectangle()
.frame(width: 100, height: 200)
.foregroundColor(.red)
NavigationLink(destination: Destination()) {
Rectangle() // I only want this Rectangle to be tappable.
.frame(width: 50, height: 100)
.foregroundColor(.red)
}
}
}
}
}
}
struct Destination: View {
var body: some View {
Text("Hello")
}
}
自从它开始工作以来,我一直在尝试使用BorderlessButtonStyle()
,但是由于某种原因,它现在不能解决我的问题。
为什么这样的行为确实会发生?
答案 0 :(得分:-1)
尝试一下。
struct ContentView: View {
@State var isSelected: Bool = false
var body: some View {
NavigationView {
VStack {
Rectangle()
.frame(width: 100, height: 200)
.foregroundColor(.red)
Button(action: {
self.isSelected.toggle()
}) {
Rectangle()
.frame(width: 100, height: 50)
.foregroundColor(.red)
.cornerRadius(10)
}
NavigationLink(destination: SecondView(), isActive: self.$isSelected) {
EmptyView()
}
}
.navigationBarTitle("First Page")
}
}
}
struct SecondView: View {
var body: some View {
VStack {
Text("Second Page")
}
.navigationBarTitle("Second Page")
}
}