Skip to content

Instantly share code, notes, and snippets.

@emarashliev
Created April 15, 2014 08:33
Show Gist options
  • Select an option

  • Save emarashliev/10713970 to your computer and use it in GitHub Desktop.

Select an option

Save emarashliev/10713970 to your computer and use it in GitHub Desktop.
Centered UIView by AutoLayout
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:view];
UIView *superView = view.superview;
superView.backgroundColor = [UIColor redColor];
view.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *variables = NSDictionaryOfVariableBindings(view, superView);
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[superView]-(<=1)-[view(178)]"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:variables]];
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[superView]-(<=1)-[view(256)]"
options: NSLayoutFormatAlignAllCenterY
metrics:nil
views:variables]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment