简体   繁体   English

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

[英]UIScrollView not scrolling to bottom in iOS 7

In this following code example self.contentView refers to the UIScrollView in question. 在下面的代码示例中, 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];
}

Oddly, in iOS 6 this works perfectly fine, but in iOS 7 the scroll view (assuming it has a contentSize that's vertically larger than it's frame.size.height ) only scrolls to the very bottom of the bottom most subview added to the scroll view. 奇怪的是,在iOS 6中,这工作得很好,但是在iOS 7中,滚动视图(假设它的contentSize垂直大于frame.size.height )仅滚动到添加到滚动视图的最底部子视图的最底部。

For example, if the following cases hold true: 例如,如果以下情况成立:

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

The scrolling code will only scroll until aSubView is visible; 滚动代码将一直滚动到可见aSubView为止; self.contentView.contentOffset.y == 20.0 rather than self.contentView.contentOffset.y == 50.0 which would be at the bottom of the entire scroll view. self.contentView.contentOffset.y == 20.0而不是self.contentView.contentOffset.y == 50.0 ,它位于整个滚动视图的底部。

This is (of course) occurs until programmatically another subview is added to self.contentView (via a user interaction), then everything corrects itself. (当然)这会发生,直到通过编程将另一个子视图添加到self.contentView (通过用户交互),然后一切都会自行纠正。

For clarity, I set breakpoints before and after the scrolling code to measure changes to self.contentView.contentOffset . 为了清楚起见,我在滚动代码前后设置了断点,以测量对self.contentView.contentOffset更改。

Other fun fact, if I remove animated and set the contentOffset directly it works as expected on iOS 7, but I'd prefer keeping the animation. 另一个有趣的事实是,如果我删除animated并直接设置contentOffset ,它将在iOS 7上按预期工作,但我更喜欢保留动画。

NOTE: Not using interface builder 注意: 不使用界面生成器

仅一行..您可以滚动到底部..!

[yourScrollview scrollRectToVisible:CGRectMake(yourScrollview.contentSize.width - 1, yourScrollview.contentSize.height - 1, 1, 1) animated:YES];

So I figured out a pretty unsatisfying solution rather quickly by wrapping the call in an async dispatch block. 因此,我通过将调用包装在异步调度块中,很快找到了一个不令人满意的解决方案。

// 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];
    });
}

If anyone understands what is really causing the problem and can provide a better solution I'd gladly accept that as the answer, but for everyone else dealing with the same issue hopefully this works for you as well. 如果有人了解真正导致问题的原因并可以提供更好的解决方案,我很乐意接受这作为答案,但对于处理同一问题的其他所有人,也希望这对您也有用。

Disabling "Adjust Scroll View Insets" solved this for me. 禁用“调整滚动视图插图”可以为我解决此问题。 (Xcode 6, iOS 8) (Xcode 6,iOS 8)

UIScrollView不滚动

You can deselecting 'Use Autolayout' in the File Inspector pane of main view within the Scroll View.This may help u. 您可以在滚动视图内主视图的文件检查器窗格中取消选择“使用自动布局”。这可能对您有所帮助。 UIScrollView doesn't scroll after upgrading to iOS7 / xcode 5 升级到iOS7 / xcode 5后,UIScrollView不滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM