SwiftUI 点击分段控件时如何 dismiss 键盘

10

在SwiftUI中,我有一个TextField需要根据由SegmentedControl()选择器确定的@State变量的值使用不同的键盘。

当用户点击不同的段时,如何关闭键盘(例如发送endEditing事件)?我需要这样做是因为我想要更改键盘类型,如果textField是响应者,则键盘不会更改。

我有这个扩展:

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

我可以做类似这样的事情

UIApplication.shared.endEditing()

但我不知道在用户点击不同的选项时应该在哪里或如何调用它。

我尝试在Picker上放置一个tapGesture,键盘确实消失了,但是点击事件没有传递到Picker,因此它没有改变。

代码片段如下:

@State private var type:String = "name"
请提供需要翻译的内容。
Form {
    Section(header: Text("Search Type")) {
        Picker("", selection: $type) {
            Text("By Name").tag("name")
            Text("By AppId").tag("id")
        }.pickerStyle(SegmentedPickerStyle())
    }

    Section(header: Text("Enter search value")) {
        TextField(self.searchPlaceHolder, text: $searchValue)
            .keyboardType(self.type == "name" ? UIKeyboardType.alphabet : UIKeyboardType.numberPad)
    }
}
4个回答

10
将自定义的“绑定”附加到“Picker”,每当它被设置时调用“endEditing()”。
Section(header: Text("Search Type")) {
    Picker("", selection: Binding(get: {
        self.type
    }, set: { (res) in
        self.type = res
        UIApplication.shared.endEditing()
    })) {
        Text("By Name").tag("name")
        Text("By AppId").tag("id")
    }.pickerStyle(SegmentedPickerStyle())
}

完美。谢谢。 - Stewart Lynch

7

自 iOS 13 / iPadOS 13 发布以来的更新。

由于现在支持一个应用程序中的多个窗口,因此您需要逐个循环遍历 UIWindows 并结束编辑。

UIApplication.shared.windows.forEach { $0.endEditing(false) }

2

SwiftUI 2.0

现在,使用 .onChange 可以更加优雅地完成这项工作(实际上它可以连接到任何视图,但是出于意图,它看起来适用于TextField)。

TextField("Placeholder", text: $searchValue)
    .keyboardType(self.type == "name" ? UIKeyboardType.alphabet : UIKeyboardType.numberPad)
    .onChange(of: type) { _ in
        UIApplication.shared.endEditing()   // << here !!
    }

SwiftUI 1.0+

这些方法与上述方法非常相似。

a) 需要 import Combine...

TextField("Placeholder", text: $searchValue)
    .keyboardType(self.type == "name" ? UIKeyboardType.alphabet : UIKeyboardType.numberPad)
    .onChange(of: type) { _ in
        UIApplication.shared.endEditing()
    }
    .onReceive(Just(type)) { _ in
        UIApplication.shared.endEditing()   // << here !!
    }

b) ... and not

TextField("Placeholder", text: $searchValue)
    .keyboardType(self.type == "name" ? UIKeyboardType.alphabet : UIKeyboardType.numberPad)
    .onReceive([type].publisher) { _ in
        UIApplication.shared.endEditing()   // << here !!
    }

-1

对于SwiftUI 3,请使用FocusState和.focused(…)。


1
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Fredrik A.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接