UITableView的小幅上升键盘隐藏时 [英] UITableView slightly goes up when keyboard hides

查看:95
本文介绍了UITableView的小幅上升键盘隐藏时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的UITableView(chatTable)用的UITabBar(chatTabBar)和内ImageView的1文本框一起。我使用的自动布局。我用下面的code键更改视图时,键盘的出现和消失。

I am using UITableView (chatTable) along with UITabBar (chatTabBar) and one textField inside imageView. I am using autolayout. I used the following code to change the views when keyboard appears and disappears.

        - (void)keyboardWasShown:(NSNotification*)aNotification
        {
            NSDictionary* info = [aNotification userInfo];

            // get animation info from userInfo
            NSTimeInterval animationDuration;
            CGRect keyboardFrame;
            [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
            [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

            // resize the frame
            [UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

                self.keyboardHeight.constant =  keyboardFrame.size.height - TABBAR_HEIGHT ;
                [self.view layoutIfNeeded];
            } completion:nil];

            if ([chatData count] != VALUE_ZERO)
            {
                [chatTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([chatData count] - VALUE_ONE) inSection:VALUE_ZERO] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
            }
        }

        - (void)keyboardWillHide:(NSNotification*)aNotification
        {
            NSDictionary* info = [aNotification userInfo];

            // get animation info from userInfo
            NSTimeInterval animationDuration;
            CGRect keyboardFrame;
            [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
            [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

            // Set view frame
            [UIView animateWithDuration:animationDuration delay:2.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                self.keyboardHeight.constant -= keyboardFrame.size.height - TABBAR_HEIGHT;
                [self.view layoutIfNeeded];
            } completion:nil];
        }

现在,当我preSS返回tableview中去了LITTEL位(从屏幕2屏幕3)。 keyboardHeight是使用TabBar和主视图之间的底部空间的限制。

Now when I press return the tableview goes up a littel bit (from screen 2 to screen 3). keyboardHeight is the bottom space constraint between the tabBar and main view.


(屏幕2)

(screen 2)


(画面3)

(screen3)

我已经尝试了很多事情,但我能不能找到原因的tableview是往上走了一段时间。 (问题是,没有流畅的动画效果。)(注:我已经把延迟2.0只显示在下面的截图(屏幕3)othewise它的价值会发生什么0)

I have tried many things but I can't able to find why the tableview is going up for a while. (problem is there is no smooth animation.) (Note: I have put delay as 2.0 only to show what happens in following screenshot(screen 3) othewise it's value would be 0)

推荐答案

您的问题是,你改变表格视图框的键盘出现时,这是不对的。你需要改变,而不是与帧插手表视图的contentInset属性。

Your problem is that you're changing the table view frame when the keyboard appears, which is wrong. You need to change the contentInset property of the table view, instead of meddling with frames.

- (void)keyboardWillShow:(NSNotification *)notification {
  CGFloat height = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height - self.tabBarController.tabBar.frame.size.height;
  UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0.0f, 0.0f, height, 0.0f);
  _tableView.contentInset = edgeInsets;
  _tableView.scrollIndicatorInsets = edgeInsets;
}

- (void)keyboardWillHide:(NSNotification *)notification {
  UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
  _tableView.contentInset = edgeInsets;
  _tableView.scrollIndicatorInsets = edgeInsets;
}

这篇关于UITableView的小幅上升键盘隐藏时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆