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

来源:互联网 发布:淘宝卖家骗局大全 编辑:程序博客网 时间:2024/05/19 17:48
<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;
    //doing something    self.editHeight = keyboardSize.height; //重置当前键盘高度    //根据键盘高度 改变 输入框和表格 的位置    [self changeInputViewTableViewPlaceWith:self.editHeight];}#pragma mark 键盘隐藏时- (void)keyboardWillBaHidden:(NSNotification *)notification {
<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]; } }];}


                                             
0 0
原创粉丝点击