Created
June 17, 2015 02:47
-
-
Save bitristan/ed9cbcea62c43b4eac57 to your computer and use it in GitHub Desktop.
IOS autolayout code snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// AutoLayoutDemo | |
// | |
// Created by Ting Sun on 6/17/15. | |
// Copyright (c) 2015 sunting. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@property (nonatomic, strong) UIView *redView; | |
@property (nonatomic, strong) UIView *yellowView; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
self.redView = [UIView new]; | |
self.redView.translatesAutoresizingMaskIntoConstraints = NO; | |
self.redView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0]; | |
self.yellowView = [UIView new]; | |
self.yellowView.translatesAutoresizingMaskIntoConstraints = NO; | |
self.yellowView.backgroundColor = [UIColor colorWithRed:1.00 green:0.83 blue:0.58 alpha:1.0]; | |
[self.view addSubview:self.redView]; | |
[self.view addSubview:self.yellowView]; | |
NSDictionary *viewsDictionary = @{@"redView":self.redView, @"yellowView":self.yellowView}; | |
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(50)]" | |
options:0 | |
metrics:nil | |
views:viewsDictionary]; | |
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(100)]" | |
options:0 | |
metrics:nil | |
views:viewsDictionary]; | |
[self.redView addConstraints:constraint_H]; | |
[self.redView addConstraints:constraint_V]; | |
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[redView]" | |
options:0 | |
metrics:nil | |
views:viewsDictionary]; | |
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]" | |
options:0 | |
metrics:nil | |
views:viewsDictionary]; | |
[self.view addConstraints:constraint_POS_V]; | |
[self.view addConstraints:constraint_POS_H]; | |
NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[yellowView(200)]" | |
options:0 | |
metrics:nil | |
views:viewsDictionary]; | |
NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[yellowView(100)]" | |
options:0 | |
metrics:nil | |
views:viewsDictionary]; | |
[self.yellowView addConstraints:yellow_constraint_H]; | |
[self.yellowView addConstraints:yellow_constraint_V]; | |
constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]-10-[yellowView]" | |
options:0 | |
metrics:nil | |
views:viewsDictionary]; | |
[self.view addConstraints:constraint_POS_H]; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment