Skip to content

Instantly share code, notes, and snippets.

@veritech
Last active August 29, 2015 14:08
Show Gist options
  • Save veritech/3005788135ecf241e52f to your computer and use it in GitHub Desktop.
Save veritech/3005788135ecf241e52f to your computer and use it in GitHub Desktop.
- (void)updateConstraints {
NSMutableArray *constraints = [NSMutableArray array];
NSDictionary *views;
NSDictionary *metrics = @{};
void (^squareConstraint)(UIView *) = ^(UIView *aView) {
[constraints addObject:[NSLayoutConstraint constraintWithItem:aView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:aView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
};
//Iterate in threes
for (NSUInteger idx=0; idx < self.subviews.count; idx++) {
if (idx % 3 == 0 && idx >= 1 && idx <= 9) {
views = @{
@"upperLeft": (idx > 3) ? self.subviews[idx-3] : [NSNull null], //Get the left most view on the upper row
@"left":self.subviews[idx-2],
@"center":self.subviews[idx-1],
@"right":self.subviews[idx]
};
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=1)-[left]-[center(==left)]-[right(==left)]-(>=1)-|"
options:NSLayoutFormatAlignAllCenterY|NSLayoutFormatAlignAllTop|NSLayoutFormatAlignAllBottom
metrics:metrics
views:views]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:views[@"center"]
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
if (views[@"upperLeft"] == [NSNull null]) {
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[left]"
options:0
metrics:metrics
views:views]];
} else {
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[upperLeft]-[left]"
options:0
metrics:metrics
views:views]];
}
}
if (idx == self.subviews.count -1 ) {
//Place the '0' and delete button
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[zero]-[delete(==zero)]"
options:NSLayoutFormatAlignAllCenterY|NSLayoutFormatAlignAllTop|NSLayoutFormatAlignAllBottom
metrics:metrics
views:@{
@"zero":self.subviews.firstObject,
@"delete":self.subviews.lastObject
}]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[upperCenter]-[zero(==upperCenter)]-|"
options:NSLayoutFormatAlignAllCenterX
metrics:metrics
views:@{
@"zero":self.subviews.firstObject,
@"upperCenter":views[@"center"]
}]];
}
//Make them all square
squareConstraint(self.subviews[idx]);
}
[self addConstraints:constraints];
[super updateConstraints];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment