在iOS 7中编辑UITextView时,UITableView不能滚动到正确的位置
我有一个静态单元格的表格视图。 一个单元格包含一个UITextView
,而heightForRowAtIndexPath:
是dynamic计算的,因此单元格总是高到足以容纳文本(该部分实际上是在iOS 7下工作的,实际上,因为不再可能只需要向textView提供contentSize
) 。
当我点击文本视图开始编辑,键盘animation到位,tableView上的contentInsets会自动调整,以解决这个问题(即216px的底部插入为iPhone的纵向),光标/脱字符变得可见,和那么表格视图会滚动到另一个位置 。 它最终看起来像一个反弹。
以下是模拟器中的video: https : //www.dropbox.com/s/htdbb0t7985u6n4/textview-bounce.mov
请注意,光标一秒钟就在键盘上方。 我一直在logging表视图的contentOffset
,我可以看到它滚动到一个很好的价值,然后突然“转身”,并回滚。
奇怪的是,如果我在模拟器中打开慢animation,问题就会消失; contentOffset
逆转不会发生,事情工作正如我所料(即,iOS 6的行为)。
这里是慢animation的video: https : //www.dropbox.com/s/nhn7vspx86t4exb/textview-nobounce.mov
实施说明:
- 文本视图是粉红色的,并具有AutoLayout约束,保持固定在距离0的单元(除了左侧,即10点)
- 我正在使用
boundingRectWithSize:
来计算表视图的高度,调整lineFragmentPadding和任何顶部/底部的插图。 似乎工作。 - 我已经将
scrollEnabled
设置为不可滚动,但scrollEnabled
== YES时没有注意到任何不同 - 这是一个表视图控制器,并
automaticallyAdjustsScrollViewInsets
ScrollViewInsets ==是
尝试在出现键盘时调整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]; } }