UIScrollview + autolayout似乎无法在iOS8 / Xcode 6预览中使用?

时间:2014-07-17 05:11:15

标签: uiscrollview autolayout ios8

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

  1. 将滚动视图添加到根视图。
  2. 将零空格固定到超级视图的所有边缘。
  3. 将UIView(contentView)添加到上面的scrollview。
  4. 将零空格固定到scrollview的所有边缘
  5. 向contentView添加一些小部件,并将contentView的高度更改为2000。
  6. =>这个contentView在iOS 7中滚动,但我无法在iOS 8预览中使用相同的步骤。

    即使它似乎在iOS 7中工作,我可能也没有做正确的方法?有什么建议吗?

2 个答案:

答案 0 :(得分:7)

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

编辑这已在种子5中修复,因此应忽略此注释!

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

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

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

- (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上可以 NOT 滚动。进一步的调查表明这是因为内容大小保持在(0,0);它不尊重内容视图的绝对宽度和高度限制。

答案 1 :(得分:0)

对UIScrollView + AutoLayout

使用以下步骤
  1. 将滚动视图添加到根视图
  2. 将包含视图添加到上方滚动视图
  3. 为滚动视图添加以下约束

    1. 尾随空格到超级视图= 0
    2. 领先空间到超级视图= 0
    3. 超级视图的顶部空间= 0
    4. 底部空间到超级视图= 0
    5. 添加以下约束以包含滚动视图的视图 (在这种情况下滚动视图是超级视图)

      1. 尾随空格到超级视图= 0
      2. 领先空间到超级视图= 0
      3. 超级视图的顶部空间= 0
      4. 底部空间到超级视图= 0
      5. 包含视图的高度(如果使用垂直滚动),否则包含视图的宽度(如果使用水平滚动)。
      6. 水平慢跑对齐(如果使用垂直滚动),否则垂直慢跑对齐(如果使用水平滚动)。