微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TextField "Intro" 点击键盘 SwiftUI (iOS 14)

如何解决TextField "Intro" 点击键盘 SwiftUI (iOS 14)

当您按下 intro 并使用 keyboardSwiftUI关闭 TextField 时,您是否尝试过处理该事件?

TextField("type your name here",text: $yourName)

我如何知道用户 dismiss keyboard 何时按下了“介绍”按钮?

非常感谢!

enter image description here

解决方法

您可以使用 onCommit 中的 TextField 参数。下面是一个例子:

TextField("type your name here",text: $yourName,onCommit: {
    print("return key pressed")
})

来自文档:

/// [...]
///   - onCommit: An action to perform when the user performs an action
///     (for example,when the user presses the Return key) while the text
///     field has focus.
public init(_ titleKey: LocalizedStringKey,text: Binding<String>,onEditingChanged: @escaping (Bool) -> Void = { _ in },onCommit: @escaping () -> Void = {})
,

在声明 TextField 后,添加一个闭包并用以下代码填充它:UIApplication.shared.keyWindow?.endEditing(true)

整体看起来是这样

TextField($yourText){
    UIApplication.shared.keyWindow?.endEditing(true)
}

此视频在 8:00 左右进行了解释。

https://www.youtube.com/watch?v=TfJ70vHABjs

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。