增加点击事件
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKey)];
[_myTableView addGestureRecognizer:tap];
触发方法
- (void)hiddenKey {
[self.view endEditing:YES];
}
防止键盘被覆盖
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
偏移
#pragma mark - keybord(监测键盘响应方法)
- (void)keyboardWillShow:(NSNotification *)notification {
[UIView animateWithDuration:0.25 animations:^{
[_myTableView setContentOffset:CGPointMake(0, 300)];
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)notification {
[UIView animateWithDuration:0.25 animations:^{
[_myTableView setContentOffset:CGPointMake(0, 0)];
} completion:nil];
}