【发布时间】:2021-04-25 18:15:04
【问题描述】:
此问题与How to hide keyboard when using SwiftUI?有关
示例代码:
struct ContentView: View {
@State var text = ""
var body: some View {
Background {
NavigationView {
List {
TextField("TextInput", text: $text)
Text("OnTapGesture")
.onTapGesture {
print("Text Tapped") //works
}
Button("Tap me") {
print("Btn Tapped") //doesn't work
}
NavigationLink("Go to detail", destination: Text("DetailView")) //doesn't work
}
}
}
}
}
struct Background<Content: View>: View {
private var content: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
Rectangle()
.overlay(content)
.onTapGesture {
self.endEditing()
}
}
func endEditing() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
这里的覆盖按预期工作,我可以点击任意位置来关闭键盘。但是,通过在叠加层上添加onTapGesture,某些其他操作不再起作用。这是故意的吗?它是一个错误吗?有没有办法解决这个问题?
【问题讨论】:
标签: ios swift swiftui keyboard uiresponder