Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UIScrollView + Auto Layout, no frames no contentSize
UIScrollView *scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|"
options:0
metrics:nil
views:@{@"scrollView" : scrollView}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|"
options:0
metrics:nil
views:@{@"scrollView" : scrollView}]];
UIImageView *previousImageView = nil;
for (int i = 0; i < 10; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:imageView];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]"
options:0
metrics:nil
views:@{@"imageView" : imageView}]];
if (!previousImageView) {
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]"
options:0
metrics:nil
views:@{@"imageView" : imageView}]];
} else {
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previousImageView][imageView]"
options:0
metrics:nil
views:@{@"imageView" : imageView,
@"previousImageView" : previousImageView}]];
}
previousImageView = imageView;
}
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previousImageView]|"
options:0
metrics:nil
views:@{@"previousImageView" : previousImageView}]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousImageView]|"
options:0
metrics:nil
views:@{@"previousImageView" : previousImageView}]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment