我正在使用UITableView(chatTable)和UITabBar(chatTabBar)以及imageView中的一个textField。 我正在使用autolayout。 当键盘出现和消失时,我使用以下代码更改视图。
- (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]; }现在,当我按下返回时,tableview上升了一小部分(从屏幕2到屏幕3)。 keyboardHeight是tabBar和主视图之间的底部空间约束。
(屏幕2)
(SCREEN3)
我尝试了很多东西,但是我无法找到为什么tableview会持续一段时间。 (问题是没有平滑的动画。)(注意:我只把延迟设为2.0,以显示后面的截图(屏幕3)中发生的情况,否则它的值将为0)
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]; }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.
(screen 2)
(screen3)
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属性,而不是使用框架进行干预。
- (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; }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; }更多推荐
发布评论