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

后端 未结 10 2000
独厮守ぢ
独厮守ぢ 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:45

    Typically in this scenario you will have a scroll view holding the rest of the User Interface. If you assign the delegate of that scroll view to your view controller, and say that you implement the protocol UIScrollViewDelegate, then just adding the following method will do the job:

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        [self.view endEditing:YES];
    }
    

提交回复
热议问题