gpt4 book ai didi

ios - 键盘隐藏时 UITableView 略微上升

转载 作者:行者123 更新时间:2023-12-01 18:15:41 26 4
gpt4 key购买 nike

enter image description here

我正在使用 UITableView (chatTable) 以及 UITabBar (chatTabBar) 和 imageView 内的一个 textField。我正在使用自动布局。当键盘出现和消失时,我使用以下代码更改 View 。

        - (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 和主视图之间的底部空间约束。

enter image description here
(屏幕 2)

enter image description here
(屏幕3)

我已经尝试了很多东西,但我无法找到为什么 tableview 会上升一段时间。 (问题是没有流畅的动画。)(注意:我将延迟设置为 2.0 只是为了显示以下屏幕截图(屏幕 3)中发生的情况,否则它的值为 0)

最佳答案

您的问题是当键盘出现时您正在更改表格 View 框架,这是错误的。您需要更改 table view 的 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;
}

关于ios - 键盘隐藏时 UITableView 略微上升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22223869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com