Skip to content

Instantly share code, notes, and snippets.

@dutran90
Created August 18, 2015 16:11
Show Gist options
  • Save dutran90/ef413e2608fd9489af3c to your computer and use it in GitHub Desktop.
Save dutran90/ef413e2608fd9489af3c to your computer and use it in GitHub Desktop.
Autolayout Programming
// Width constraint, half of parent view width
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:0]];
// Height constraint, half of parent view height
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
// Center horizontally
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
// NSLayoutAttributeLeading
// NSLayoutAttributeTrailing
// NSLayoutAttributeTop
// NSLayoutAttributeBottom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment