UIScrollview自动布局似乎无法在iOS8 / Xcode 6预览中使用?

UIScrollView autolayout的以下步骤对我有用,但不适用于iOS8 / Xcode 6预览:(使用storyboard,启用了大小等级):

>将滚动视图添加到根视图.
>将零空间固定到超视图的所有边缘.
>在上面的scrollview中添加一个UIView(contentView).
>将零空格固定到scrollview的所有边缘
>向contentView添加一些小部件,并将contentView的高度更改为2000.

=>这个contentView在iOS 7中滚动,但我无法在iOS 8预览中使用相同的步骤.

即使它似乎在iOS 7中工作,我可能不会采取正确的方式吗?有什么建议?

解决方法

我很惊讶没有看到更多关于此的评论.滚动视图内部自动布局在iOS 8中大部分被破坏(截至撰写本文时为止).

编辑这是在种子5中修复的,所以应该忽略这个注释!

该规则应该是(参见https://developer.apple.com/library/prerelease/ios/technotes/tn2154/_index.html),如果滚动视图(其子视图或子视图)的内容固定到滚动视图的所有四个边界,则它设置内容大小.

然而,在iOS 8中,这失败了 – 但是以一种奇怪的方式.只有当确定子视图的高度和宽度的约束都是绝对的而不是内在的时,它才会失败.

因此,例如,考虑该技术说明底部代码,其中滚动视图和一个非常大的图像视图在代码中创建(这里是;我已经纠正了一个丢弃了at符号的小错误):

- (void)viewDidLoad {
    UIScrollView *scrollView;
    UIImageView *imageView;
    NSDictionary *viewsDictionary;
    // Create the scroll view and the image view.
    scrollView  = [[UIScrollView alloc] init];
    imageView = [[UIImageView alloc] init];
    // Add an image to the image view.
    [imageView setimage:[UIImage imageNamed:@"MyReallyBigImage"]];
    // Add the scroll view to our view.
    [self.view addSubview:scrollView];
    // Add the image view to the scroll view.
    [scrollView addSubview:imageView];
    // Set the translatesAutoresizingMaskIntoConstraints to NO so that the views
    // autoresizing mask is not translated into auto layout constraints.
    scrollView.translatesAutoresizingMaskIntoConstraints  = NO;
    imageView.translatesAutoresizingMaskIntoConstraints = NO;
    // Set the constraints for the scroll view and the image view.
    viewsDictionary = NSDictionaryOfVariableBindings(scrollView,imageView);
    [self.view addConstraints:[NSLayoutConstraint 
        constraintsWithVisualFormat:@"H:|[scrollView]|" 
        options:0 metrics: 0 views:viewsDictionary]];
    [self.view addConstraints:[NSLayoutConstraint 
        constraintsWithVisualFormat:@"V:|[scrollView]|" 
        options:0 metrics: 0 views:viewsDictionary]];
    [scrollView addConstraints:[NSLayoutConstraint 
        constraintsWithVisualFormat:@"H:|[imageView]|" 
        options:0 metrics: 0 views:viewsDictionary]];
    [scrollView addConstraints:[NSLayoutConstraint 
        constraintsWithVisualFormat:@"V:|[imageView]|" 
        options:0 metrics: 0 views:viewsDictionary]];
}

代码有效(假设您的图像非常大),因为图像视图的大小受内在约束的影响.但现在更改最后两行,如下所示:

[scrollView addConstraints:[NSLayoutConstraint 
        constraintsWithVisualFormat:@"H:|[imageView(1000)]|" 
        options:0 metrics: 0 views:viewsDictionary]];
    [scrollView addConstraints:[NSLayoutConstraint 
        constraintsWithVisualFormat:@"V:|[imageView(1000)]|" 
        options:0 metrics: 0 views:viewsDictionary]];

现在你所拥有的是一个可在iOS 7上滚动但在iOS 8上不可滚动的滚动视图.进一步的调查显示这是因为内容大小保持在(0,0);它不尊重内容视图的绝对宽度和高度约束.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...