how can I dismiss keyboard when I will just touch anywhere except UITextField?

后端 未结 10 1996
独厮守ぢ
独厮守ぢ 2021-02-19 11:12

Hello I have two UITextFields in my application, and want to dismiss the keyboard when I just touch anywhere except the UITextFields how can I do this?

10条回答
  •  臣服心动
    2021-02-19 11:50

    If you are not using Interface builder, you can manually hook up events as explained here: How can I programmatically link an UIView or UIImageView with an event like "touch up inside"?

    If you are using Interface builder, then you don't need to create a background button.

    You could do this:

    1. Select your view (probably called View)
    2. Change to the (i) tab in the property inspector
    3. Change the class from UIView to UIControl (this gives you access to the Touch Down event).
    4. Hook the Touch Down event to your backgroundTap method (Ctrl + drag).
    5. Don't forget to Save changes

提交回复
热议问题