我正在尝试学习如何在UIScrollView中使用Autolayout。我找到了一个文件,解释了一个非常好的案例,发现here。
现在我扩展了这个示例以加载多个图像,现在我发现滚动大小不会自动调整大小?它只发生在第一次。我做错了什么,或者这只是它的方式?这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
UIScrollView *scroll = [[UIScrollView alloc] init];
[scroll setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:scroll];
NSDictionary *views = @{@"widget" : scroll};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[widget]|" options:0 metrics: 0 views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[widget]|" options:0 metrics: 0 views:views]];
UIImageView *last = nil;
for (int i = 0; i < 5; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImage:[UIImage imageNamed:@"afl.jpg"]];
[imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[scroll addSubview:imageView];
if(last){
views = @{@"image" : imageView, @"last" : last};
[scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[image]|" options:0 metrics: 0 views:views]];
[scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[last]-[image]" options:0 metrics: 0 views:views]];
}else{
views = @{@"image" : imageView};
[scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[image]|" options:0 metrics: 0 views:views]];
[scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[image]|" options:0 metrics: 0 views:views]];
}
last = imageView;
}
}
通过这个例子,我可以看到下一张图像在我弹跳时开始,但滚动大小禁止我向下滚动。
答案 0 :(得分:0)
终于得到了......我一直在寻找很长时间(在发布这个问题之前。所以不要删除这个问题。我会回答它。你不必设置内容大小的原因是因为它自动将其设置为适合放入第一个对象的任何内容。下一个图像将显示在它下面,但是scrollview不会重置其内容大小。所以你必须更新约束。我这样做的方式是在我的代码中,只有两个图像(不会让问题变得更难):
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
UIScrollView *scroll = [[UIScrollView alloc] init];
[scroll setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:scroll];
NSDictionary *views = @{@"widget" : scroll};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[widget]|" options:0 metrics: 0 views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[widget]|" options:0 metrics: 0 views:views]];
UIImageView *last = nil;
NSArray *hConstraint = nil;
NSArray *vConstraint = nil;
for (int i = 0; i < 2; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImage:[UIImage imageNamed:@"afl.jpg"]];
[imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[scroll addSubview:imageView];
if(last){
views = @{@"image" : imageView, @"last" : last};
[scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[image]|" options:0 metrics: 0 views:views]];
vConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[last]-[image]|" options:0 metrics: 0 views:views];
[scroll updateConstraints];
}else{
views = @{@"image" : imageView};
hConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[image]|" options:0 metrics: 0 views:views];
vConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[image]|" options:0 metrics: 0 views:views];
[scroll addConstraints:hConstraint];
[scroll addConstraints:vConstraint];
}
last = imageView;
}
}
所以基本上我更新了绑定到内容大小的高度约束
答案 1 :(得分:0)
这篇文章通过逐步说明更详细地解释了使用AutoLayout
与UIScrollView
的对比。