【发布时间】:2014-02-13 01:34:22
【问题描述】:
在以下代码示例中,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];
}
奇怪的是,在 iOS 6 中这工作得非常好,但在 iOS 7 中,滚动视图(假设它有一个垂直大于 frame.size.height 的 contentSize)只会滚动到添加到的最底部子视图的最底部滚动视图。
例如,如果以下情况成立:
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
滚动代码只会滚动到aSubView 可见; self.contentView.contentOffset.y == 20.0 而不是 self.contentView.contentOffset.y == 50.0,后者将位于整个滚动视图的底部。
这(当然)会发生,直到以编程方式将另一个子视图添加到 self.contentView(通过用户交互),然后一切都会自行纠正。
为了清楚起见,我在滚动代码之前和之后设置断点以测量对self.contentView.contentOffset 的更改。
其他有趣的事实,如果我删除 animated 并直接设置 contentOffset 它在 iOS 7 上按预期工作,但我更喜欢保留动画。
注意:不使用界面生成器
【问题讨论】:
标签: ios iphone objective-c uiscrollview contentoffset