iOS – Auto Layouts

Auto-layouts are introduced in iOS 6.0. When we are about to use auto-layouts, our deployment target must be 6.0 and higher. Auto-layouts assists us in creating interfaces that can be employed for multiple orientations and for multiple devices.

What is the Goal of Our Example?

We will add two buttons that will be placed in a certain distance from the middle of the screen. We will also try to add a resizable text field that will be placed from a given distance from above the buttons.

What is Our Approach?

We will add a text field and two buttons in the code also with their constraints. The constraints of every UI Elements will be created and added to the super view. We will have to disable auto-resizing for every of the UI elements we add in order to get the desired output.

What are the Steps Involved?

  • Step 1. Create a simple view-based application.
  • Step 2. We will edit only ViewController.m and it is as follows −
1
#import "ViewController.h"
2
@interface ViewController ()
3
@property (nonatomic, strong) UIButton *leftButton;
4
@property (nonatomic, strong) UIButton *rightButton;
5
@property (nonatomic, strong) UITextField *textfield;
6
@end
7
@implementation ViewController
8
- (void)viewDidLoad{
9
  [super viewDidLoad];
10
  UIView *superview = self.view;
11
  /*1. Create leftButton and add to our view*/
12
  self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
13
  self.leftButton.translatesAutoresizingMaskIntoConstraints = NO;
14
  [self.leftButton setTitle:@"LeftButton" forState:UIControlStateNormal];
15
  [self.view addSubview:self.leftButton];
16
  /* 2. Constraint to position LeftButton's X*/
17
  NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
18
  constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX
19
  relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
20
  NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];
21
  /* 3. Constraint to position LeftButton's Y*/
22
  NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
23
  constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterY
24
  relatedBy:NSLayoutRelationEqual toItem:superview attribute:
25
  NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
26
  /* 4. Add the constraints to button's superview*/
27
  [superview addConstraints:@[ leftButtonXConstraint,
28
  leftButtonYConstraint]];
29
  /*5. Create rightButton and add to our view*/
30
  self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
31
  self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;
32
  [self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal];
33
  [self.view addSubview:self.rightButton];
34
  /*6. Constraint to position RightButton's X*/
35
  NSLayoutConstraint *rightButtonXConstraint = [NSLayoutConstraint
36
  constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterX
37
  relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
38
  NSLayoutAttributeCenterX multiplier:1.0 constant:60.0f];
39
  /*7. Constraint to position RightButton's Y*/
40
  rightButtonXConstraint.priority = UILayoutPriorityDefaultHigh;
41
  NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint
42
  constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterY
43
  relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
44
  NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
45
  [superview addConstraints:@[centerYMyConstraint,
46
  rightButtonXConstraint]];
47
 //8. Add Text field
48
  self.textfield = [[UITextField alloc]initWithFrame:
49
  CGRectMake(0, 100, 100, 30)];
50
  self.textfield.borderStyle = UITextBorderStyleRoundedRect;
51
  self.textfield.translatesAutoresizingMaskIntoConstraints = NO;
52
  [self.view addSubview:self.textfield];
53
  //9. Text field Constraints
54
  NSLayoutConstraint *textFieldTopConstraint = [NSLayoutConstraint
55
  constraintWithItem:self.textfield attribute:NSLayoutAttributeTop
56
  relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview
57
  attribute:NSLayoutAttributeTop multiplier:1.0 constant:60.0f];
58
  NSLayoutConstraint *textFieldBottomConstraint = [NSLayoutConstraint
59
  constraintWithItem:self.textfield attribute:NSLayoutAttributeTop
60
  relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.rightButton
61
  attribute:NSLayoutAttributeTop multiplier:0.8 constant:-60.0f];
62
  NSLayoutConstraint *textFieldLeftConstraint = [NSLayoutConstraint
63
  constraintWithItem:self.textfield attribute:NSLayoutAttributeLeft
64
  relatedBy:NSLayoutRelationEqual toItem:superview attribute:
65
  NSLayoutAttributeLeft multiplier:1.0 constant:30.0f];
66
  NSLayoutConstraint *textFieldRightConstraint = [NSLayoutConstraint
67
  constraintWithItem:self.textfield attribute:NSLayoutAttributeRight
68
  relatedBy:NSLayoutRelationEqual toItem:superview attribute:
69
  NSLayoutAttributeRight multiplier:1.0 constant:-30.0f];
70
  [superview addConstraints:@[textFieldBottomConstraint ,
71
  textFieldLeftConstraint, textFieldRightConstraint,
72
  textFieldTopConstraint]];
73
}
74
- (void)didReceiveMemoryWarning
75
{
76
  [super didReceiveMemoryWarning];
77
  // Dispose of any resources that can be recreated.
78
}
79
@end
80

Points to Note

In steps marked 1, 5, and 8, we just programmatically added two buttons and a text field respectively.

In the remaining steps, we created constraints and added them to the respective super views, which are actually self-views. The constraints of one of the left buttons is as shown below −


1
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
2
constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX
3
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
4
NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];
5

We have constraintWithItem and toItem which will decide between which UI elements we are creating the constraint. The attribute will decide on what basis the two elements are joined together. “relatedBy” will decide how much effect the attributes will have between the elements. Multiplier is the multiplication factor and constant will be added to the multipler.

In the above example, the X of leftButton is always greater than or equal to -60 pixels with respect to the center of the super view. In the same way, other constraints are also defined.

Output

After running the application, we’ll get the below output on the iPhone simulator −

iOS - Auto Layouts

When we alter the orientation of the simulator to landscape, we will get the below output −

iOS - Auto Layouts

After running the same application on iPhone 5 simulator, we will get the below output −

iOS - Auto Layouts

When we alter the orientation of the simulator to landscape, we will get the below output −

iOS - Auto Layouts