Skip to content

Instantly share code, notes, and snippets.

@nandodelauni
Last active December 31, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nandodelauni/7945061 to your computer and use it in GitHub Desktop.
Save nandodelauni/7945061 to your computer and use it in GitHub Desktop.
UIScrollView category to add images using auto layout (visual format) to handle contentSize automatically
#import "UIScrollView+Images.h"
@implementation UIScrollView (Images)
- (void)addImages:(NSArray *)images
{
NSMutableDictionary *viewsDictionary = [NSMutableDictionary dictionary];
NSMutableString *visualFormatString = [NSMutableString stringWithString:@"|"];
NSUInteger index = 0;
for (UIImage *anImage in images) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
[self addSubview:imageView];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
NSString *key = [NSString stringWithFormat:@"imageView%d", index];
[visualFormatString appendFormat:@"[%@]", key];
[viewsDictionary setObject:imageView forKey:key];
index++;
}
[visualFormatString appendString:@"|"];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualFormatString options:0 metrics:nil views:viewsDictionary]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment