UIScrollView + autolayout的以下步骤对我有用,但不适用于iOS8 / Xcode 6预览:(使用storyboard,启用了尺寸等级):
=>这个contentView在iOS 7中滚动,但我无法在iOS 8预览中使用相同的步骤。
即使它似乎在iOS 7中工作,我可能也没有做正确的方法?有什么建议吗?
答案 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
使用以下步骤为滚动视图添加以下约束
添加以下约束以包含滚动视图的视图 (在这种情况下滚动视图是超级视图)