How do I dismiss the iOS keyboard?

后端 未结 16 1115
情书的邮戳
情书的邮戳 2020-11-29 20:00

I have a UITextfield that i\'d like to dismiss the keyboard for. I can\'t seem to make the keyboard go away no matter what code i use.

16条回答
  •  旧巷少年郎
    2020-11-29 20:06

    To resign any text field in the app

    UIApplication.shared.keyWindow?.endEditing(true)
    

    This approach is clean and guarantied to work because the keyWindow is, by definition, the root view of all possible views displaying a keyboard (source):

    The key window receives keyboard and other non-touch related events. Only one window at a time may be the key window.

提交回复
热议问题