前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >iOS_监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移

iOS_监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移

作者头像
mikimo
发布2022-07-20 13:33:14
发布2022-07-20 13:33:14
85900
代码可运行
举报
文章被收录于专栏:iOS开发~iOS开发~
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
复制
<pre name="code" class="objc"><pre name="code" class="objc">#pragma mark view将要显示时
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //注册监听键盘显隐通知
    //键盘出现时
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShow:) name:UIKeyboardDidShowNotification object:nil];
    //键盘隐藏时
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBaHidden:) name:UIKeyboardDidHideNotification object:nil];
    //根据键盘高度 改变 输入框和表格 的位置
    [self changeInputViewTableViewPlaceWith:self.currentKeyboardHeight];
}

#pragma mark 键盘显示时
- (void)keyboardWasShow:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];
    //获得键盘尺寸
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
代码语言:javascript
代码运行次数:0
复制
    //doing something
    self.editHeight = keyboardSize.height; //重置当前键盘高度
    //根据键盘高度 改变 输入框和表格 的位置
    [self changeInputViewTableViewPlaceWith:self.editHeight];
}

#pragma mark 键盘隐藏时
- (void)keyboardWillBaHidden:(NSNotification *)notification {
代码语言:javascript
代码运行次数:0
复制
<pre name="code" class="objc">    //doing something

}#pragma mark 根据键盘高度 改变 输入框和表格 的位置- (void)changeInputViewTableViewPlaceWith:(CGFloat)height { [self.inputView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.bottom.equalTo(self.view).offset(-height); make.height.mas_equalTo(kInputHeight); }]; [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(64); make.width.equalTo(self.view); make.bottom.equalTo(self.inputView.mas_top); //使tableView滑到最下端 NSInteger arrCount = self.messagesArray.count; NSIndexPath *index = [NSIndexPath indexPathForRow:arrCount - 1 inSection:0]; if (arrCount > 0) { [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } if (height > kMoreHeight) { CGFloat showhHeight = kHeight - kInputHeight - height - 64; CGFloat allHeight = self.tableView.contentSize.height; CGPoint contentPoint = CGPointMake(0, allHeight - showhHeight); [self.tableView setContentOffset:contentPoint animated:YES]; } }];}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-03-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档