SwiftUI - 在视图中的任意位置点击时关闭键盘 - 其他交互元素的问题 [英] SwiftUI - dismissing keyboard on tapping anywhere in the view - issues with other interactive elements
问题描述
我在视图中有一个 TextField
和一些可操作的元素,例如 Button
、Picker
.当使用在 TextField
之外点击时,我想关闭键盘.使用这个问题中的答案,我做到了.然而,问题在于其他可操作的项目.
当我点击 Button
时,动作发生,但键盘没有关闭.与 Toggle
开关相同.当我点击 SegmentedStyle Picker
的一部分时,键盘会关闭,但选择器选择不会改变.
这是我的代码.
<预><代码>struct SampleView:查看{@State var 选择 = 0@State var textFieldValue = ""var主体:一些视图{VStack(间距:16){TextField("请输入您的姓名", text: $textFieldValue).padding(EdgeInsets(顶部:8,领先:16,底部:8,尾随:16)).background(颜色(UIColor.secondarySystemFill)).cornerRadius(4)Picker(selection: $selected, label: Text(""), content: {文本(字").标签(0)文本(短语").标签(1)文本(句子").标签(2)}).pickerStyle(SegmentedPickerStyle())按钮(动作:{self.textFieldValue = "点击按钮"}, 标签: {Text("点击更改文本")})}.填充().onTapGesture(执行:UIApplication.dismissKeyboard)//.gesture(TapGesture().onEnded { _ in UIApplication.dismissKeyboard()})}}公共扩展 UIApplication {静态功能解雇键盘(){让 keyWindow = shared.connectedScenes.filter({$0.activationState == .foregroundActive}).map({$0 as? UIWindowScene}).compactMap({$0}).first?.windows.filter({$0.isKeyWindow}).firstkeyWindow?.endEditing(true)}}正如您在代码中看到的,我尝试了两个选项来获取点击手势,但没有任何效果.
您可以像这样在 View 上创建扩展
扩展视图{func endTextEditing() {UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder),to: nil, from: nil, for: nil)}}
并将其用于要关闭键盘的视图.
.onTapGesture {self.endTextEditing()}
我刚刚在最近的 raywenderlich 教程中看到了这个解决方案,所以我认为它是目前最好的解决方案.
I have a TextField
and some actionable elements like Button
, Picker
inside a view. I want to dismiss the keyboard when the use taps outside the TextField
. Using the answers in this question, I achieved it. However the problem comes with other actionable items.
When I tap a Button
, the action takes place but the keyboard is not dismissed. Same with a Toggle
switch.
When I tap on one section of a SegmentedStyle Picker
, the keyboard is dimissed but the picker selection doesn't change.
Here is my code.
struct SampleView: View {
@State var selected = 0
@State var textFieldValue = ""
var body: some View {
VStack(spacing: 16) {
TextField("Enter your name", text: $textFieldValue)
.padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
.background(Color(UIColor.secondarySystemFill))
.cornerRadius(4)
Picker(selection: $selected, label: Text(""), content: {
Text("Word").tag(0)
Text("Phrase").tag(1)
Text("Sentence").tag(2)
}).pickerStyle(SegmentedPickerStyle())
Button(action: {
self.textFieldValue = "button tapped"
}, label: {
Text("Tap to change text")
})
}.padding()
.onTapGesture(perform: UIApplication.dismissKeyboard)
// .gesture(TapGesture().onEnded { _ in UIApplication.dismissKeyboard()})
}
}
public extension UIApplication {
static func dismissKeyboard() {
let keyWindow = shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
keyWindow?.endEditing(true)
}
}
As you can see in the code, I tried both options to get the tap gesture and nothing worked.
You can create an extension on View like so
extension View {
func endTextEditing() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder),
to: nil, from: nil, for: nil)
}
}
and use it for the Views you want to dismiss the keyboard.
.onTapGesture {
self.endTextEditing()
}
I have just seen this solution in a recent raywenderlich tutorial so I assume it's currently the best solution.
这篇关于SwiftUI - 在视图中的任意位置点击时关闭键盘 - 其他交互元素的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!