【问题标题】:TextField "Intro" Tap on the keyboard SwiftUI (iOS 14)TextField "Intro" 点击键盘 SwiftUI (iOS 14)
【发布时间】:2021-05-22 23:04:25
【问题描述】:

您是否曾尝试在使用TextFieldSwiftUI 中按introkeyboard 关闭时处理该事件?

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

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

非常感谢!

【问题讨论】:

    标签: swiftui keyboard textfield ios14


    【解决方案1】:

    您可以使用来自TextFieldonCommit 参数。这是一个例子:

    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 = {})
    

    【讨论】:

    • 非常感谢!
    【解决方案2】:

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

    总之就是这样

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

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

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

    【讨论】:

    • onCommit 允许您在用户按下“返回键”时进行处理,这就是我想要的......无论如何我真的很感谢你的回答
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 2021-08-04
    • 2020-05-17
    • 2021-01-05
    • 1970-01-01
    • 2020-02-09
    相关资源
    最近更新 更多