Skip to content

Instantly share code, notes, and snippets.

@joewest
Created January 24, 2014 17:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joewest/8602228 to your computer and use it in GitHub Desktop.
Save joewest/8602228 to your computer and use it in GitHub Desktop.
Center a subview in a superview using Auto Layout's visual format language O_o
UIView *superview = self.view;
NSDictionary *variables = NSDictionaryOfVariableBindings(label, superview);
NSArray *constraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[label]"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:variables];
[self.view addConstraints:constraints];
constraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[label]"
options: NSLayoutFormatAlignAllCenterY
metrics:nil
views:variables];
[self.view addConstraints:constraints];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment