UIScrollView的错自动布局偏移 [英] UIScrollView wrong offset with Auto Layout

查看:435
本文介绍了UIScrollView的错自动布局偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的视图配置:

I have a fairly simple view configuration:

A 的UIViewController ,带着孩子的UIScrollView 的UIImageView 在这个的UIScrollView
我设置了的UIImageView 有足够的高度打出来的可见区域(即更高 1024pt ),并设置底部空间的SuperView我的的UIImageView 来一个固定的正值(约束20 为例)。

A UIViewController, with a child UIScrollView and a UIImageView in this UIScrollView. I set the UIImageView with a height sufficient to break out of the visible area (ie. higher to 1024pt), and set the Bottom space to superview constraint of my UIImageView to a fixed positive value (20 for example).

整个安装工作​​正常,图像很好地滚动在其父。
除了当视图被滚动(效果更明显,如果您滚动到视图的底部),然后消失,再次出现(您切换到另外的看法,回来)滚动值将被恢复,但其中的内容滚动视图移动到它的父视图外顶部。

The whole setup works as expected, the image scrolls nicely in its parent. Except when the view is scrolled (the effect is more visible if you scrolled to the bottom of the view), then disappear, and appear again (you switched to another view and came back) the scrolling value is restored, but the content of the scroll view is moved to the outside top part of its parent view.

这不是简单的解释,我会尽力把它画:

This is not simple to explain, I'll try to draw it:

如果你想测试/查看源(或故事板,我没有编辑code单行)。我把一个小演示在我github上:<一href=\"https://github.com/guillaume-algis/iOSAutoLayoutScrollView\">https://github.com/guillaume-algis/iOSAutoLayoutScrollView

If you want to test/view the source (or the storyboard, I did not edit a single line of code). I put a little demo on my github: https://github.com/guillaume-algis/iOSAutoLayoutScrollView

我没有阅读 iOS 6的更新日志和在这个特定主题的解释,并认为这是正确的执行了第二个选项(自动纯布局),但在这种情况下,为什么是的UIScrollView 表现如此不正常?我缺少的东西吗?

I did read the iOS 6 changelog and the explanation on this particular topic, and think this is the correct implementation of the second option (pure auto layout), but in this case why is the UIScrollView behaving so erratically ? Am I missing something ?

修改:这是完全相同的问题,因为#12580434的UIScrollView,自动布局 - 问题。这些问题的答案都只是解决方法,因为任何人都找到了正确的方法来解决这个问题或者这是一个错误的iOS?

EDIT: This is the exact same issue as #12580434 uiscrollview-autolayout-issue. The answers are just workarounds, as anyone found a proper way to fix this or is this a iOS bug ?

编辑2 :我发现了另外一个解决方法,它保持用户离开时的相同状态滚动位置(这是结束的 12580434 的接受的答案):

EDIT 2: I found another workaround, which keep the scroll position in the same state the user left it (this is an improvement over 12580434's accepted answer):

@interface GAViewController ()

@property CGPoint tempContentOffset;

@end


@implementation GAViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.tempContentOffset = self.mainScrollView.contentOffset;
    self.scrollView.contentOffset = CGPointZero;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.scrollView.contentOffset = self.tempContentOffset;
}

这基本上保存在 viewWillAppear中偏移,其重置到原点,然后恢复 viewDidAppear 价值。这个问题似乎这两个调用之间发生,但我找不到它的起源。

This basically save the offset in viewWillAppear, reset it to the origin, and then restore the value in viewDidAppear. The problem seems to occur between these two calls, but I can't find its origin.

推荐答案

呀,奇怪的事情发生与UIScrollView的纯自动布局环境。重读的iOS SDK 6.0版本说明为第二十次我发现:

Yeah, something strange happened with UIScrollView in pure autolayout environment. Re-reading the iOS SDK 6.0 release notes for the twentieth time I found that:

请注意,您可以滚动视图的子视图出现通过创建视图和滚动视图的子树外的视图,如滚动视图的上海华之间的约束浮动(不滚动)比其他滚动内容。

Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.

您的子视图连接到外部视图。换句话说,在其中滚动视图嵌入的图。

由于IB不容许我们建立的ImageView和滚动视图的子树外面的景色,之间的约束,如滚动视图的SuperView然后我在code做了。

As IB does not allow us set up constraints between the imageView and a view outside the scroll view’s subtree, such as the scroll view’s superview then I've done it in code.

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    [self.view removeConstraints:[self.view constraints]];
    [self.scrollView removeConstraints:[self.scrollView constraints]];
    [self.imageView removeConstraints:[self.imageView constraints]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_imageView(700)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imageView(1500)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
}

和VAU!它的工作原理!

And vau! It works!

这篇关于UIScrollView的错自动布局偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆