UITableView在iOS 7中,在单元格中编辑UITextView时,不滚动到正确的位置

前端之家收集整理的这篇文章主要介绍了UITableView在iOS 7中,在单元格中编辑UITextView时,不滚动到正确的位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个静态单元的表视图.一个单元格包含一个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

实施说明:

>文本视图是粉红色的,并且具有AutoLayout约束,将其固定在距离0的单元格(除了左侧,这是10pts)
>我使用boundingRectWithSize:计算表视图高度,调整lineFragmentPadding和任何顶/底插入.似乎工作
>我已经将textView设置为不可滚动,但在scrollEnabled == YES时没有注意到任何不同
>这是一个表视图控制器,并且自动调用ScrollViewInsets == YES

解决方法

键盘出现时尝试调整UITableView框架.在viewWillAppear中调用[self attachKeyboardHelper],并在viewWillDisappear中调用[self detachKeyboardHelper].
  1. - (void)attachKeyboardHelper{
  2. [[NSNotificationCenter defaultCenter] addObserver:self
  3. selector:@selector(keyboardWillAppear:)
  4. name:UIKeyboardWillShowNotification
  5. object:nil];
  6. [[NSNotificationCenter defaultCenter] addObserver:self
  7. selector:@selector(keyboardWillHide:)
  8. name:UIKeyboardWillHideNotification
  9. object:nil];
  10. }
  11.  
  12. - (void)detachKeyboardHelper{
  13. [[NSNotificationCenter defaultCenter] removeObserver:self];
  14. }
  15.  
  16. - (void)keyboardWillAppear:(NSNotification *)notification{
  17. NSDictionary* userInfo = [notification userInfo];
  18.  
  19. NSTimeInterval animationDuration;
  20. UIViewAnimationCurve animationCurve;
  21. CGRect keyboardEndFrame;
  22.  
  23. [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
  24. [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
  25. [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
  26.  
  27. // Animate up or down
  28. [UIView beginAnimations:nil context:nil];
  29. [UIView setAnimationDuration:animationDuration];
  30.  
  31. CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
  32. if(self.view==self.tableView){
  33. 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);
  34. self.tableView.frame = newTableFrame;
  35. }else{
  36. CGRect newTableFrame = CGRectMake(self.tableView.frame.origin.x,self.view.bounds.size.height-self.tableView.frame.origin.y-keyboardFrame.size.height);
  37. self.tableView.frame = newTableFrame;
  38. }
  39.  
  40. [UIView commitAnimations];
  41. }
  42.  
  43. - (void)keyboardWillHide:(NSNotification *)notification{
  44. NSDictionary* userInfo = [notification userInfo];
  45.  
  46. NSTimeInterval animationDuration;
  47. UIViewAnimationCurve animationCurve;
  48. CGRect keyboardEndFrame;
  49.  
  50. [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
  51. [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
  52. [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
  53.  
  54.  
  55. CGRect newTableFrame = CGRectMake(self.tableView.frame.origin.x,self.view.superview.bounds.size.height-self.tableView.frame.origin.y);
  56. self.tableView.frame = newTableFrame;
  57. if(newTableFrame.size.height>self.tableView.contentSize.height-self.tableView.contentOffset.y){
  58. float newOffset=MAX(self.tableView.contentSize.height-newTableFrame.size.height,0);
  59. [self.tableView setContentOffset:CGPointMake(0,newOffset) animated:YES];
  60. }
  61.  
  62. }

猜你在找的iOS相关文章