iOS ViewController点击空白处收起键盘

点击ViewController空白处时收起键盘,可以通过系统的touchesBegan方法实现,具体如下:

Objective-C 版

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

Swift 版

// 收起键盘
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}