Programatically Set Autolayout Constraints in iOS.

By | July 6, 2015

Here is a simple programs that creates two buttons programatically and sets its constraints.

Auto Layout

Auto Layout

#import "ViewController.h"
 
@interface ViewController ()
 
@property (nonatomic, strong) UIButton *myButton;
@property (nonatomic, strong) UIButton *myButton2;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *superview = self.view;
    /* Create myButton and add to our view*/
    self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.myButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.myButton setTitle:@"Test" forState:UIControlStateNormal];
    [self.view addSubview:self.myButton];
    /* Constraint to position myButton's X*/
    NSLayoutConstraint *myButtonXConstraint = [NSLayoutConstraint
                                                 constraintWithItem:self.myButton attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
                                                 NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];
    /* Constraint to position myButton's Y*/
    NSLayoutConstraint *myButtonYConstraint = [NSLayoutConstraint
                                                 constraintWithItem:self.myButton attribute:NSLayoutAttributeCenterY
                                                 relatedBy:NSLayoutRelationEqual toItem:superview attribute:
                                                 NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
    /* Add the constraints to button's superview*/
    [superview addConstraints:@[ myButtonXConstraint, myButtonYConstraint]];
    self.myButton.backgroundColor = [UIColor greenColor];
     
    /* Create myButton2 and add to our view*/
    self.myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.myButton2.translatesAutoresizingMaskIntoConstraints = NO;
    [self.myButton2 setTitle:@"Test" forState:UIControlStateNormal];
    [self.view addSubview:self.myButton2];
    /* Constraint to position myButton2's X*/
    NSLayoutConstraint *myButton2XConstraint = [NSLayoutConstraint
                                                  constraintWithItem:self.myButton2 attribute:NSLayoutAttributeCenterX
                                                  relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
                                                  NSLayoutAttributeCenterX multiplier:1.0 constant:60.0f];
    /* Constraint to position myButton2's Y*/
    myButton2XConstraint.priority = UILayoutPriorityDefaultHigh;
    NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint
                                               constraintWithItem:self.myButton2 attribute:NSLayoutAttributeCenterY
                                               relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
                                               NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
    [superview addConstraints:@[centerYMyConstraint,  myButton2XConstraint]];
     self.myButton.backgroundColor = [UIColor greenColor];
}

Leave a Reply

Your email address will not be published. Required fields are marked *