iOS 显示键盘时的 UITableView contentOffset
iOS UITableView contentOffset when keyboard is shown
我正在尝试在出现 keyboard
时设置 tableView
的 contentOffset
。实际代码在 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];
}
}];
}
问题在于,当用户点击靠近 tableView
顶部的 tableCell
时,它总是将内容偏移键盘的高度,这是错误的。
理想情况下,我想偏移内容,使点击的 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;
}];
我正在尝试在出现 keyboard
时设置 tableView
的 contentOffset
。实际代码在 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];
}
}];
}
问题在于,当用户点击靠近 tableView
顶部的 tableCell
时,它总是将内容偏移键盘的高度,这是错误的。
理想情况下,我想偏移内容,使点击的 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;
}];