我有一个带静态单元格的表视图。一个单元格包含UITextView
,heightForRowAtIndexPath:
是动态计算的,因此单元格总是高到足以容纳文本(该部分在iOS 7下完成了一些工作,实际上,因为它不再可能简单请求textView查看其contentSize
)。
当我在文本视图中点击开始编辑时,键盘动画到位,tableView上的contentInsets会自动调整以解决此问题(即,对于iPhone纵向方向,216px的底部插入),光标/插入符号变为可见,然后表视图滚动到另一个位置。它最终看起来像是反弹。
以下是模拟器中的视频:https://www.dropbox.com/s/htdbb0t7985u6n4/textview-bounce.mov
请注意,插入符号位于键盘上方一秒钟。我一直在记录表视图的contentOffset
,我可以看到它滚动到一个很好的值,然后突然“转身”并向后滚动。
奇怪的是,如果我在模拟器中打开慢动画,问题就会消失; contentOffset
逆转不会发生,事情就像我期望的那样(即iOS 6行为)。
以下是动画效果较慢的视频:https://www.dropbox.com/s/nhn7vspx86t4exb/textview-nobounce.mov
实施说明:
boundingRectWithSize:
计算表格视图高度,调整lineFragmentPadding和任何顶部/底部插图。似乎工作。scrollEnabled
== YES automaticallyAdjustsScrollViewInsets
== YES 答案 0 :(得分:3)
键盘出现时尝试调整UITableView框架。在viewWillAppear中调用[self attachKeyboardHelper]
,在viewWillDisappear中调用[self detachKeyboardHelper]
。
- (void)attachKeyboardHelper{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)detachKeyboardHelper{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillAppear:(NSNotification *)notification{
NSDictionary* userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
// Animate up or down
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
if(self.view==self.tableView){
CGRect newTableFrame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.view.bounds.size.height-keyboardFrame.size.height);
self.tableView.frame = newTableFrame;
}else{
CGRect newTableFrame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.view.bounds.size.height-self.tableView.frame.origin.y-keyboardFrame.size.height);
self.tableView.frame = newTableFrame;
}
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification{
NSDictionary* userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
CGRect newTableFrame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.view.superview.bounds.size.height-self.tableView.frame.origin.y);
self.tableView.frame = newTableFrame;
if(newTableFrame.size.height>self.tableView.contentSize.height-self.tableView.contentOffset.y){
float newOffset=MAX(self.tableView.contentSize.height-newTableFrame.size.height, 0);
[self.tableView setContentOffset:CGPointMake(0, newOffset) animated:YES];
}
}