iOS ViewController点击空白处收起键盘

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

Objective-C 版

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

Swift 版

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

你可能感兴趣的:(iOS ViewController点击空白处收起键盘)