1,在viewdidload里面 写一个方法
[self registerKeyBoardAction];
2,实现这个方法
#pragma mark - 注册键盘弹起与消失事件
-(void)registerKeyBoardAction{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillHideNotifcation:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShowNotifcation:) name:UIKeyboardWillShowNotification object:nil];
}
-(void)keyBoardWillHideNotifcation:(NSNotification *)notifcation{
// [_detailsTX resignFirstResponder];
[self.view resignFirstResponder];
[self.view endEditing:YES];
_myTableView.contentInset=UIEdgeInsetsZero;
}
#pragma mark - 键盘消失与显示通知方法
-(void)keyBoardWillShowNotifcation:(NSNotification *)notifcation{
self.view.frame = [[UIScreen mainScreen] bounds];
//获取键盘的高度
NSValue *value = notifcation.userInfo[@"UIKeyboardFrameEndUserInfoKey"];
CGRect keyBoardFrame;
[value getValue:&keyBoardFrame];
_myTableView.contentInset=UIEdgeInsetsMake(0, 0,keyBoardFrame.size.height, 0);
}
以上方法不好用
第二种 是针对textview键盘弹起
-(void)textViewDidBeginEditing:(UITextView *)textView{
if(textView == _textview){
_textview = (JSTextView *)textView;
}
float offset = 0.0f;
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard"context:nil];
[UIView setAnimationDuration:animationDuration];
float width = _myTableView.frame.size.width;
float height = _myTableView.frame.size.height;
CGRect rect = CGRectMake(0.0f, offset-30 , width, height);
_myTableView.frame = rect;
[UIView commitAnimations];
}
-(void)textViewDidEndEditing:(UITextView *)textView{
[UIView beginAnimations:@"ResizeForKeyBoard"context:nil];
[UIView setAnimationDuration:0.3f];
CGRect rect = CGRectMake(0.0f, 64 , SCREENWIDTH, SCREENHEIGHT-64);
_myTableView.frame = rect;
[UIView commitAnimations];
}
不好用
第三种是第三方
IQKeyboardManager 是第三方
@property (nonatomic, strong) IQKeyboardReturnKeyHandler *returnKeyHandler;
self.returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
self.returnKeyHandler.lastTextFieldReturnKeyType = UIReturnKeyDone;
[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;