ios - 如何检测 iOS 中 UITextView (UIScrollView) 故障的底部

标签 ios uiscrollview uitextview

我正在使用延迟加载程序,我遇到了一个问题,我的 UITextView 认为它在我加载更多数据后立即位于 View 的底部,因此错误地设置了我的偏移量。

我只是尝试加载新数据,并让 UITextView 稍微向下滚动,以便用户知道已加载更多数据。

代码:

-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
  float scrollViewHeight = scrollView.frame.size.height;
  float scrollContentSizeHeight = scrollView.contentSize.height;
  float yOffset = scrollView.contentOffset.y;
  NSLog(@"yOffset + scrollViewHeight = %f",yOffset + scrollViewHeight);
  NSLog(@"yOffset = %f",yOffset );

  if (yOffset == 0) {
    // then we are at the top
  }
  else if (yOffset + scrollViewHeight == scrollContentSizeHeight) {
    if (self.updateInProgress) {
      NSLog(@"updateInProgress");
      return;
    }
    // then we are at the end
    self.updateInProgress = TRUE;
    NSLog(@"scrollContentSizeHeight = %f",scrollView.contentSize.height);
    NSLog(@" === at bottom of scrollview ===");

    // JUST NEED TO FIX WHERE SCROLLVIEW APPEARS!
    self.TextView.text = [NSString stringWithFormat:@"%@\n\n%@",self.TextView.text, self.nextChapterString];
    CGPoint offset = self.TextView.contentOffset;
    offset.y = yOffset + scrollViewHeight + 100;
    [self.TextView setContentOffset:offset animated:NO];
    //NSLog(@"updated TextView = %@", self.TextView.text);
    // since disabled scrolling while updating, force scroll down a bit
    //    CGPoint offset = scrollView.contentOffset;
    //    offset.y += 100;
    //    [scrollView setContentOffset:offset animated:YES];

  }
  self.updateInProgress = FALSE;
}

输出:

这是我看到的输出,如您所见,加载更多数据后我的 yOffset 发生了巨大变化...

2014-05-07 23:11:40.370 Test[6314:60b] yOffset + scrollViewHeight = 1821.500000
2014-05-07 23:11:40.370 Test[6314:60b] yOffset = 1432.500000
2014-05-07 23:11:40.371 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.403 Test[6314:60b] yOffset + scrollViewHeight = 1821.000000
2014-05-07 23:11:40.403 Test[6314:60b] yOffset = 1432.000000
2014-05-07 23:11:40.404 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.470 Test[6314:60b] yOffset + scrollViewHeight = 1820.500000
2014-05-07 23:11:40.471 Test[6314:60b] yOffset = 1431.500000
2014-05-07 23:11:40.471 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.504 Test[6314:60b] yOffset + scrollViewHeight = 1820.000000
2014-05-07 23:11:40.505 Test[6314:60b] yOffset = 1431.000000
2014-05-07 23:11:40.505 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.506 Test[6314:60b] scrollContentSizeHeight = 1820.000000
2014-05-07 23:11:40.506 Test[6314:60b]  === at bottom of scrollview ===
2014-05-07 23:11:40.508 Test[6314:60b] yOffset + scrollViewHeight = 2309.000000
2014-05-07 23:11:40.508 Test[6314:60b] yOffset = 1920.000000
2014-05-07 23:11:40.508 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.509 Test[6314:60b] yOffset + scrollViewHeight = 1954.000000
2014-05-07 23:11:40.509 Test[6314:60b] yOffset = 1565.000000
2014-05-07 23:11:40.510 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.510 Test[6314:60b] scrollContentSizeHeight = 1954.000000
2014-05-07 23:11:40.510 Test[6314:60b]  === at bottom of scrollview ===
2014-05-07 23:11:40.512 Test[6314:60b] yOffset + scrollViewHeight = 2443.000000
2014-05-07 23:11:40.512 Test[6314:60b] yOffset = 2054.000000
2014-05-07 23:11:40.513 Test[6314:60b] yOffset = 389.000000

关于我做错了什么有什么建议吗?

最佳答案

试试这个

    text.text = [NSString stringWithFormat:@"%@\n\n%@",text.text, str];
    CGPoint offset = text.contentOffset;
    offset.y = yOffset + scrollViewHeight + 100;
    text.delegate = nil;//set your delegate nil
    [text setContentOffset:offset animated:NO];
    text.delegate = self;//set back

关于ios - 如何检测 iOS 中 UITextView (UIScrollView) 故障的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23532224/

相关文章:

ios - UITextView resignFirstResponder 不工作

iphone - 什么类型的应用程序允许在 iPhone 中自动启动?

ios - 表格单元格约束无法正确加载

ios - 基于 UIScrollView 的 contentOffset 改变颜色

iphone - 应用程序在 [scrollView removeFromSuperview] 上崩溃;

swift - 在 Swift 3.0 中检测 TextView 滚动以显示按钮

iphone - UITextView 末尾的省略号

iphone - 在 xcode 中创建自定义 segue

ios - 如果单元格是 UITableViewCell,我应该将单元格出队而不是分配它吗?

ios - 当键盘出现时,快速 UITableView 向上滚动,但我看不到顶部单元格