Last active
November 19, 2015 21:09
-
-
Save arkilis/db3181182eabeab64395 to your computer and use it in GitHub Desktop.
iOS Layout
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 | |
// testLayout | |
// | |
// Created by Ben Liu on 19/11/2015. | |
// Copyright (c) 2015 Ben Liu. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// self.view is the super view | |
UIView *superview = self.view; | |
UIView *view1 = [[UIView alloc] init]; | |
view1.translatesAutoresizingMaskIntoConstraints = NO; | |
view1.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0]; | |
[superview addSubview:view1]; | |
UIEdgeInsets padding = UIEdgeInsetsMake(40, 10, 10, 10); | |
[superview addConstraints:@[ | |
//view1 constraints | |
// I found out by using UI native Auto Layout you must have the 4 contraints at | |
// At the same time | |
[NSLayoutConstraint constraintWithItem:view1 | |
attribute:NSLayoutAttributeTop | |
relatedBy:NSLayoutRelationEqual | |
toItem:superview | |
attribute:NSLayoutAttributeTop | |
multiplier:1.0 | |
constant:padding.top], | |
[NSLayoutConstraint constraintWithItem:view1 | |
attribute:NSLayoutAttributeLeading | |
relatedBy:NSLayoutRelationEqual | |
toItem:superview | |
attribute:NSLayoutAttributeLeft | |
multiplier:1.0 | |
constant:padding.left], | |
[NSLayoutConstraint constraintWithItem:view1 | |
attribute:NSLayoutAttributeBottom | |
relatedBy:NSLayoutRelationEqual | |
toItem:superview | |
attribute:NSLayoutAttributeBottom | |
multiplier:0.5 // half of the height of superview | |
constant:-padding.bottom], | |
[NSLayoutConstraint constraintWithItem:view1 | |
attribute:NSLayoutAttributeTrailing | |
relatedBy:NSLayoutRelationEqual | |
toItem:superview | |
attribute:NSLayoutAttributeRight | |
multiplier:1 | |
constant:-padding.right], | |
]]; | |
// 0x03. Add another view | |
UIView *view2 = [[UIView alloc] init]; | |
view2.translatesAutoresizingMaskIntoConstraints = NO; | |
view2.backgroundColor = [UIColor colorWithRed:1.00 green:0.83 blue:0.58 alpha:1.0]; | |
[superview addSubview:view2]; | |
UIEdgeInsets padding2 = UIEdgeInsetsMake(10, 10, 10, 10); | |
[superview addConstraints:@[ | |
//view1 constraints | |
[NSLayoutConstraint constraintWithItem:view2 | |
attribute:NSLayoutAttributeTop | |
relatedBy:NSLayoutRelationEqual | |
toItem:view1 | |
attribute:NSLayoutAttributeBottom | |
multiplier:1.0 | |
constant:padding2.top], | |
[NSLayoutConstraint constraintWithItem:view2 | |
attribute:NSLayoutAttributeLeft | |
relatedBy:NSLayoutRelationEqual | |
toItem:superview | |
attribute:NSLayoutAttributeLeft | |
multiplier:1.0 | |
constant:padding2.left], | |
[NSLayoutConstraint constraintWithItem:view2 | |
attribute:NSLayoutAttributeBottom | |
relatedBy:NSLayoutRelationEqual | |
toItem:superview | |
attribute:NSLayoutAttributeBottom | |
multiplier:1.0 | |
constant:-padding2.bottom], | |
[NSLayoutConstraint constraintWithItem:view2 | |
attribute:NSLayoutAttributeRight | |
relatedBy:NSLayoutRelationEqual | |
toItem:superview | |
attribute:NSLayoutAttributeRight | |
multiplier:1 | |
constant:-padding2.right], | |
]]; | |
} | |
- (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