显示键盘时,iOS UITableView contentOffset [英] iOS UITableView contentOffset when keyboard is shown
问题描述
我正在尝试在显示keyboard
时设置tableView
的contentOffset
.实际代码位于CustomTableView : UITableView <UIScrollViewDelegate>
内部,因为该代码在整个应用程序中得到了广泛使用,因此将self
用作tableView
.
我的代码如下:
I am trying to set the contentOffset
of a tableView
when the keyboard
is presented. The actual code is inside a CustomTableView : UITableView <UIScrollViewDelegate>
, as it is used widely throughout the app, hence the use of self
as the tableView
.
My code looks like this:
- (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];
}
}];
}
问题在于,它总是使内容偏移键盘的高度,这是错误的,当用户点击靠近tableView
顶部的tableCell
时.
理想情况下,我想偏移内容,以便点击的tableCell
恰好在键盘上方.
问题是我不知道该怎么做.我有一个大概的想法,我需要这样的东西:
The problem is that, it always offsets the content by the keyboard's height, which is wrong, when the user taps a tableCell
that is near the top of the tableView
.
Ideally, I would want to offset the content so the tapped tableCell
is just above the keyboard.
The problem is that I don't know how to do it. I have a rough idea that I would need something like:
distanceBetweenTappedCellAndTableBottom > keyboardHeight ? do nothing : offsetContentBy keyboardHeight + tappedCellHeight.
关于如何得出这些数字的任何建议?还是有更好的解决方案?
Any suggestions on how could I come up with the numbers? or is there a better solution?
以后
我不能使用contentInset
,因为tableView
具有sectionHeaders
,并且设置contentInset
会导致sectionHeaders
保持固定,而rows
向上移动.
I can't use the contentInset
as the tableView
has sectionHeaders
, and setting the contentInset
causes the sectionHeaders
to stay fixes, while the rows
move up.
解决方案,基于以下建议:
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中时,我认为键盘不会移动
I think keyboard will not move when you add keyboard height into edgeInset as well
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 UITableView contentOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!