Last active
January 3, 2016 09:59
-
-
Save jimjeffers/8446590 to your computer and use it in GitHub Desktop.
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
/** | |
* This example programatically generates a series of UIViews each | |
* 244x2000 and stacks them vertically inside of a scroll view. | |
* To do this we dynamically build a dictionary as we generate | |
* the views and simultaneously build an array of height | |
* constraints. Once the loop has completed we combine all of the | |
* constraints into a single string and apply the last constraint | |
* to the scrollview. | |
* | |
* This example assumes you're working in a UIViewController with | |
* an IBOutlet for a UIScrollView. This also assumes the scrollView | |
* has been configured with NSLayoutConstraints in a UIStoryboard. | |
* The IBOutlet in this example is simply 'scrollView' referenced | |
* below as a property of self. | |
* | |
* Keep in mind that we apply the horizontal constraint to the scroll | |
* view within the loop. This is becuase there is no horizontal | |
* relationship between the views in this example. If you were to stack | |
* the views horizontally as opposed to vertically you would apply the | |
* vertical constraint immediately within the loop and generate the | |
* horizontal constraint to be applied last. | |
*/ | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO; // Necessary if using autolayout | |
NSArray *colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]]; | |
NSMutableDictionary *viewsDictionary = [NSMutableDictionary new]; | |
NSMutableArray *heightConstraints = [NSMutableArray new]; | |
int count = 1; | |
for (UIColor *color in colors) { | |
UIView *currentColor = [UIView new]; | |
currentColor.backgroundColor = color; | |
[self.scrollView addSubview:currentColor]; | |
currentColor.translatesAutoresizingMaskIntoConstraints = NO; // Necessary if using autolayout | |
NSString *viewKey = [NSString stringWithFormat:@"color%i", count]; | |
count += 1; | |
[viewsDictionary setObject:currentColor forKey:viewKey]; | |
[heightConstraints addObject:[NSString stringWithFormat:@"[%@(2000)]",viewKey]]; | |
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|[%@(244)]|", viewKey] | |
options:0 | |
metrics:0 | |
views:viewsDictionary]]; | |
} | |
// Generate and apply @"V:|[color1(2000)][color2(2000)][color3(2000)]|" | |
NSString *verticalConstraints = [NSString stringWithFormat:@"V:|%@|", [heightConstraints componentsJoinedByString:@""]]; | |
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:verticalConstraints | |
options:0 | |
metrics:0 | |
views:viewsDictionary]]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment