当键盘隐藏时,UITableView稍微上升(UITableView slightly goes up when keyboard hides)

编程入门 行业动态 更新时间:2024-10-25 02:22:29
键盘隐藏时,UITableView稍微上升(UITableView slightly goes up when keyboard hides)

我正在使用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; }

更多推荐

本文发布于:2023-08-07 05:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1460031.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键盘   UITableView   slightly   keyboard   hides

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!