gpt4 book ai didi

ios - 显示键盘时,iOS UITableView contentOffset

转载 作者:行者123 更新时间:2023-12-01 16:03:32 24 4
gpt4 key购买 nike

我尝试在显示contentOffset时设置tableViewkeyboard。实际的代码位于CustomTableView : UITableView <UIScrollViewDelegate>内部,因为它在整个应用程序中得到了广泛使用,因此将self用作tableView
我的代码如下所示:

- (void)keyboadDidAppear:(NSNotification *)notification {
NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(keyboardBeginFrame);
CGPoint contentOffset = self.contentOffset;
originalContentOffset = self.contentOffset;//hold reference to reset it when keyboard is hidden
contentOffset.y += keyboardHeight;
[UIView animateWithDuration:animationDuration animations:^{
[self layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
[self setContentOffset:contentOffset animated:YES];
}
}];
}

问题在于,它总是使内容偏移键盘的高度,这在用户点击靠近 tableCell顶部的 tableView时是错误的。
理想情况下,我想偏移内容,以便轻按的 tableCell位于键盘上方。
问题是我不知道该怎么做。我有一个大概的想法,我需要这样的东西:
distanceBetweenTappedCellAndTableBottom > keyboardHeight ? do nothing : offsetContentBy keyboardHeight + tappedCellHeight.

关于如何得出这些数字的任何建议?还是有更好的解决方案?

以后编辑:

我无法使用 contentInset,因为 tableView具有 sectionHeaders,并且设置 contentInset会使 sectionHeaders保持固定,而 rows向上移动。

基于以下建议的解决方案:
CGSize kbSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

CGFloat height = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]) ? kbSize.width : kbSize.height;

height += 10.0;//it needs to be slightly higher so the textField is clearly visible.
originalEdgeInset = self.contentInset;
__weak typeof (self) block = self;
[UIView animateWithDuration:duration animations:^{
UIEdgeInsets edgeInsets = block.contentInset;
edgeInsets.bottom += height;
block.contentInset = edgeInsets;
edgeInsets = block.scrollIndicatorInsets;
edgeInsets.bottom += height;
block.scrollIndicatorInsets = edgeInsets;
}];

最佳答案

我认为当您将键盘高度也添加到edgeInset中时,键盘不会移动

  CGSize kbSize = [[[sender userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSTimeInterval duration = [[[sender userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

CGFloat height = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]) ? kbSize.width : kbSize.height;

[UIView animateWithDuration:duration animations:^{
UIEdgeInsets edgeInsets = _generalTableView.contentInset;
edgeInsets.bottom += height;
_generalTableView.contentInset = edgeInsets;
edgeInsets = _generalTableView.scrollIndicatorInsets;
edgeInsets.bottom += height;
_generalTableView.scrollIndicatorInsets = edgeInsets;
}];

关于ios - 显示键盘时,iOS UITableView contentOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873674/

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