UIScrollView不会在iOS 7中滚动到底部

Awe*_*e-o 1 iphone objective-c uiscrollview ios contentoffset

在下面的代码示例中self.contentView引用了相关UIScrollView问题.

// Scroll to bottom.
CGPoint bottomOffset = CGPointMake(0, self.contentView.contentSize.height - 
                                   self.contentView.bounds.size.height);
if (bottomOffset.y >= 0.0)
{
    [self.contentView setContentOffset:bottomOffset animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,在iOS 6中,这非常好用,但在iOS 7中,滚动视图(假设它contentSize的垂直大于它frame.size.height)只滚动到添加到滚动视图的最底部子视图的最底部.

例如,如果以下情况适用:

self.contentView.frame.size.height == 50.0
self.contentView.contentSize.height == 100.0

aSubView.frame.origin.y == 50.0
aSubView.frame.size.height == 20.0
Run Code Online (Sandbox Code Playgroud)

滚动代码只会滚动直到aSubView可见; self.contentView.contentOffset.y == 20.0而不是self.contentView.contentOffset.y == 50.0在整个滚动视图的底部.

这是(当然)直到以编程方式添加另一个子视图self.contentView(通过用户交互),然后一切都自行纠正.

为清楚起见,我在滚动代码之前和之后设置断点来测量更改self.contentView.contentOffset.

其他有趣的事实,如果我直接删除animated并设置contentOffset它在iOS 7上按预期工作,但我更喜欢保持动画.

注意:不使用界面构建器

AP_*_*AP_ 7

只需一行..你可以滚动到底部..!

[yourScrollview scrollRectToVisible:CGRectMake(yourScrollview.contentSize.width - 1, yourScrollview.contentSize.height - 1, 1, 1) animated:YES];
Run Code Online (Sandbox Code Playgroud)


Awe*_*e-o 5

因此,通过将调用包装在异步调度块中,我很快就找到了一个非常不令人满意的解决方案.

// Scroll to bottom.
CGPoint bottomOffset = CGPointMake(0, self.contentView.contentSize.height
                                   - self.contentView.bounds.size.height);
if (bottomOffset.y >= 0.0)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.contentView setContentOffset:bottomOffset animated:YES];
    });
}
Run Code Online (Sandbox Code Playgroud)

如果有人理解真正导致问题的原因并且可以提供更好的解决方案,我很乐意接受这个答案,但对于处理同一问题的其他人来说,希望这对您也有用.

  • 这是令人不满意的,因为代码已经在iOS 6中没有调度块的情况下工作了,而且这个代码只在主线程上执行,所以它应该在没有附加调度块的情况下工作.事实上,我必须强制程序等到当前的Runloop循环完成它在执行这行代码之前正在做的事情,这表明对调用该方法时真正发生的事情缺乏了解. (2认同)