ios 收键盘keyboard touch outside

https://stackoverflow.com/questions/5306240/iphone-dismiss-keyboard-when-touching-outside-of-uitextfield

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];
-(void)dismissKeyboard 
{
    [aTextField resignFirstResponder];
}

最外层view哪里来?
2个办法:
1.传入
2.专门写一个baseKeyboardViewController来处理键盘事件,需要做收键盘的ViewController,应该都继承这个ViewController

第二种:收textfeild外的键盘

@property (nonatomic, strong) UIGestureRecognizer *tapGesture;
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editAreaHidden)];

  • (void) editAreaHidden {
    [self.nameTextField resignFirstResponder];
    }

-(void) textFieldDidBeginEditing:(UITextField *)textField {
[self.view addGestureRecognizer:self.tapGesture];
}
-(void) textFieldDidEndEditing:(UITextField *)textField {
[self.view removeGestureRecognizer:self.tapGesture];
}

你可能感兴趣的:(ios 收键盘keyboard touch outside)