Created
August 6, 2013 00:17
-
-
Save drodriguez/6160864 to your computer and use it in GitHub Desktop.
Auto Layout equal spacing with a little math
This file contains 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
NSMutableArray *horizontalConstraints = [NSMutableArray array]; | |
// Fixed width, same width for all buttons (33) | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:left attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:33.f]]; | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:left attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:center1 attribute:NSLayoutAttributeWidth multiplier:1.f constant:0]]; | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:left attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:center2 attribute:NSLayoutAttributeWidth multiplier:1.f constant:0]]; | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:left attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:right attribute:NSLayoutAttributeWidth multiplier:1.f constant:0]]; | |
// Align baselines | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:left attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:center1 attribute:NSLayoutAttributeBaseline multiplier:1.f constant:0]]; | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:left attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:center2 attribute:NSLayoutAttributeBaseline multiplier:1.f constant:0]]; | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:left attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:right attribute:NSLayoutAttributeBaseline multiplier:1.f constant:0]]; | |
// Use 2 * superview.centerX to calculate width. | |
// Then position views at 0, 1/3, 2/3 and 1. | |
// Nudge the views a little with some margin and half-widths. | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:left attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:2.f*0.f constant:33.f/2.f + 8.f]]; | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:center1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:2.f*1.f/3.f constant:8.f]]; | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:center2 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:2.f*2.f/3.f constant:-8.f]]; | |
[horizontalConstraints addObject:[NSLayoutConstraint constraintWithItem:right attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:2.f*1.f constant:-33.f/2.f - 8.f]]; | |
// Yes, you have to do some calculations, but they are pretty easy. | |
// I get 57px, 58px, 57px in portrait and 111px, 111px and 110px in landscape. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment