Last active
December 31, 2015 05:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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