弹出键盘时UITableview内容跟着上移,不至于被键盘挡住,导致UITableView内容显示不完
Posted YouNeedCourage
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了弹出键盘时UITableview内容跟着上移,不至于被键盘挡住,导致UITableView内容显示不完相关的知识,希望对你有一定的参考价值。
//首先注册通知,监听键盘:
- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisAppear:) name:UIKeyboardWillHideNotification object:nil]; }
//在键盘弹出和收起的方法中做以下操作
- (void)keyboardWillAppear:(NSNotification *)noti { NSDictionary *userInfo = noti.userInfo; //获取键盘的frame CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; //获取键盘弹出的动画时间 NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey ] doubleValue]; //让输入框跟随变化 [UIView animateWithDuration:animationDuration animations:^{ [_ctv mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(@(-keyboardFrame.size.height)); }]; }]; //设置tableview的contentInset属性,改变tableview的显示范围 _tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardFrame.size.height, 0); _tableView.scrollEnabled = YES; } - (void)keyboardWillDisAppear:(NSNotification *)noti { NSDictionary *userInfo = noti.userInfo; NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey ] doubleValue]; [UIView animateWithDuration:animationDuration animations:^{ [_ctv mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(@0); make.height.equalTo(@52); }]; }]; UIEdgeInsets contentInsets = UIEdgeInsetsZero; _tableView.contentInset = contentInsets; }
以上是关于弹出键盘时UITableview内容跟着上移,不至于被键盘挡住,导致UITableView内容显示不完的主要内容,如果未能解决你的问题,请参考以下文章
Android 软键盘弹出时布局内指定内容上移实现及问题解决
[Cordova/Phonegap] Cordova iOS 应用在第三方输入法的键盘弹出(点击输入框)时,页面不上移,导致输入框被键盘遮挡 的解决办法