Dismiss keyboard on touch anywhere outside UITextField

前端 未结 8 1839
梦毁少年i
梦毁少年i 2020-11-27 17:22

I am developing an iPad app that has a large number of UIViewControllers, UITableViews (with cells with accessoryViews of UIText

8条回答
  •  感情败类
    2020-11-27 17:33

    Here is a much easier and efficient way of dealing with that. This is gonna work for any UITextField in your view controller. You can even add it to your base view controller (if you have got one) and it will work like a charm.

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [[event allTouches] anyObject];
    
        if (![[touch view] isKindOfClass:[UITextField class]]) {
            [self.view endEditing:YES];
        }
        [super touchesBegan:touches withEvent:event];
    }
    

提交回复
热议问题