【发布时间】:2020-01-19 05:16:53
【问题描述】:
当用户单击视图的背景区域时,我试图触发按钮。 作为一种解决方案,我决定制作一个按钮并将视图作为标签放入按钮中。 但问题是按钮即使在 TextField 上也会触发,有时也会在内部的另一个按钮上触发。
如何禁用我选择的某些视图(在这种情况下为 TextField 和 Button)上的后退按钮?
我在下面附上我的代码和rendered preview。
import SwiftUI
struct ButtonOnbutton: View {
@State var memo: String = ""
var body: some View {
Button(action: {
print("down button clicked")
}) {
VStack(spacing: 10) {
Text("before")
.foregroundColor(.white)
TextField("type memo", text: $memo)
.font(.custom("SFProDisplay-Regular", size: 12))
.textFieldStyle(RoundedBorderTextFieldStyle())
Text("after")
.foregroundColor(.white)
Button(action: {
print("inside button clicked")
}) {
Text("inside button")
.foregroundColor(.white)
}
.background(Color.red)
}
}
.background(Color.blue)
.padding()
}
}
struct buttonOnbutton_Previews: PreviewProvider {
static var previews: some View {
ButtonOnbutton()
}
}
【问题讨论】:
标签: swift swiftui ios13 xcode11 swift5