UIScrollView + autolayout的以下步骤已为我工作,但在iOS8 / Xcode 6预览中不起作用:(使用情节提要,启用了size类):
=>此contentView在iOS 7中滚动,但是在iOS 8预览中无法执行相同的步骤。
即使它似乎可以在iOS 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上不可滚动。进一步的调查表明,这是因为内容大小保持为(0,0)
; 它不遵守内容视图的绝对宽度和高度限制。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句