Sea*_*ean 8 uiscrollview autolayout ios8
UIScrollView + autolayout的以下步骤对我有用,但不适用于iOS8/Xcode 6预览:(使用storyboard,启用了大小等级):
=>此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]];
}
Run Code Online (Sandbox Code Playgroud)
该代码有效(假设您的图像非常大),因为图像视图的大小受内在约束的影响.但现在改变最后两行,如下所示:
[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]];
Run Code Online (Sandbox Code Playgroud)
现在你所拥有的是一个滚动视图,它可以在iOS 7上滚动但在iOS 8上不可滚动.进一步的调查显示这是因为内容大小保持在(0,0); 它不尊重内容视图的绝对宽度和高度约束.
| 归档时间: |
|
| 查看次数: |
8393 次 |
| 最近记录: |