Skip to content

Instantly share code, notes, and snippets.

@christianroman
Last active April 7, 2016 07:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianroman/11027662 to your computer and use it in GitHub Desktop.
Save christianroman/11027662 to your computer and use it in GitHub Desktop.
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