1.AutoresizingMask(子控件与父控件之间做适配)
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
CGFloat X = self.view.frame.size.width - 100;
CGFloat Y = self.view.frame.size.height - 100;
redView.frame = CGRectMake(X, Y, 100, 100);
/*
UIViewAutoresizingFlexibleLeftMargin 距离父控件的左边是可以伸缩的
UIViewAutoresizingFlexibleBottomMargin 距离父控件的底部是可以伸缩的
UIViewAutoresizingFlexibleRightMargin 距离父控件的右边是可以伸缩的
UIViewAutoresizingFlexibleTopMargin 距离父控件的顶部是可以伸缩的
UIViewAutoresizingFlexibleHeight 高度会跟随父控件的高度进行伸缩
UIViewAutoresizingFlexibleWidth 宽度会跟随父控件的宽度进行伸缩
*/
redView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
2.Autulayout(可解决任何控件之间的位置关系)
-
Autulayout在代码中的使用
//
// ViewController.m
// 06-Autolayout代码
//
// Created by xiaomage on 16/1/5.
// Copyright © 2016年 小码哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.蓝色的view
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
// 禁止autoresizing自动转为约束
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];
// 2.红色的view
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 禁止autoresizing自动转为约束
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];
/**** 添加约束****/
// 3.添加蓝色view的约束
// 3.1左边约束
NSLayoutConstraint *leftlcs_b = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:30];
[self.view addConstraint:leftlcs_b];
// 3.2底部约束
NSLayoutConstraint *bottomlcs_b = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-30];
[self.view addConstraint:bottomlcs_b];
// 3.3右边约束
NSLayoutConstraint *rightlcs_b = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-30];
[self.view addConstraint:rightlcs_b];
// 3.4宽度约束
NSLayoutConstraint *wlcs_b = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
[self.view addConstraint:wlcs_b];
// 3.5高度约束
NSLayoutConstraint *hlcs_b = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
[blueView addConstraint:hlcs_b];
// 4.添加红色view的约束
// 4.1 右边的约束
NSLayoutConstraint *rightlcs_r = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-30];
[self.view addConstraint:rightlcs_r];
// 4.2 顶部对齐
NSLayoutConstraint *toplcs_r = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
[self.view addConstraint:toplcs_r];
//4.3 底部对齐
NSLayoutConstraint *bottomlcs_r = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
[self.view addConstraint:bottomlcs_r];
}
- (void)test
{
// 1.红色的view
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 禁止autoresizing自动转为约束
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];
// 2.添加约束
// 2.1 宽度约束
NSLayoutConstraint *wlcs = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:100];
[redView addConstraint:wlcs];
// 2.2 高度约束
NSLayoutConstraint *hlcs = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:100];
[redView addConstraint:hlcs];
// 2.3 右边约束
NSLayoutConstraint *rlcs = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self.view addConstraint:rlcs];
// 2.4 底部约束
NSLayoutConstraint *bottomlcs = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];
[self.view addConstraint:bottomlcs];
}
@end
-
vfl的使用
//
// ViewController.m
// 07-Vfl
//
// Created by xiaomage on 16/1/5.
// Copyright © 2016年 小码哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.蓝色的view
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
// 禁止autoresizing自动转为约束
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];
// 2.红色的view
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 禁止autoresizing自动转为约束
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];
// 3.添加约束
// 3.1 水平方向
// NSDictionary *views = @{
// @"redView" : redView,
// @"blueView" : blueView
// };
NSDictionary *views = NSDictionaryOfVariableBindings(redView,blueView);
NSDictionary *metrics = @{@"space" : @30};
NSString *hvfl = @"H:|-space-[blueView]-space-[redView(==blueView)]-space-|";
NSArray *hlcs = [NSLayoutConstraint constraintsWithVisualFormat:hvfl options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views];
[self.view addConstraints:hlcs];
// 3.2 垂直方向
NSString *vvfl = @"V:[blueView(50)]-space-|";
NSArray *vlcs = [NSLayoutConstraint constraintsWithVisualFormat:vvfl options:kNilOptions metrics:metrics views:views];
[self.view addConstraints:vlcs];
// // 4.2 顶部对齐
// NSLayoutConstraint *toplcs_r = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
// [self.view addConstraint:toplcs_r];
//
// //4.3 底部对齐
// NSLayoutConstraint *bottomlcs_r = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
// [self.view addConstraint:bottomlcs_r];
}
- (void)test
{
// 1.红色的view
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 禁止autoresizing自动转为约束
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];
// 2.添加约束
// 2.1水平方向
NSString *hvfl = @"H:|-space-[abc]-space-|";
NSDictionary *views = @{@"abc" : redView};
NSDictionary *metrics = @{@"space" : @40};
NSArray *hlcs = [NSLayoutConstraint constraintsWithVisualFormat:hvfl options:kNilOptions metrics:metrics views:views];
[self.view addConstraints:hlcs];
// 2.2 垂直方向
NSString *vvfl = @"V:[abc(40)]-space-|";
NSArray *vlcs = [NSLayoutConstraint constraintsWithVisualFormat:vvfl options:kNilOptions metrics:metrics views:views];
[self.view addConstraints:vlcs];
}
@end
-
修改约束,动画
//
// ViewController.m
// 09-修改约束,动画
//
// Created by xiaomage on 16/1/5.
// Copyright © 2016年 小码哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *redViewW;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.redViewW.constant = 50;
[UIView animateWithDuration:2.0 animations:^{
// 强制刷新
[self.view layoutIfNeeded];
}];
}
@end
-####Masonry的使用
//
// ViewController.m
// 10-Masonry的使用
//
// Created by xiaomage on 16/1/5.
// Copyright © 2016年 小码哥. All rights reserved.
//
#import "ViewController.h"
//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.蓝色的view
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// 2.红色的view
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
/*** 添加新的约束 ***/
// 3.设置蓝色view的约束
[blueView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left).offset(30);
make.bottom.equalTo(self.view.bottom).offset(-30);
make.right.equalTo(redView.left).offset(-30);
make.width.equalTo(redView.width);
// make.height.equalTo(50);
}];
// 4.设置红色view的约束
[redView makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view.right).offset(-30);
make.top.equalTo(blueView.top);
make.bottom.equalTo(blueView.bottom);
}];
/*** 更新约束 ****/
[blueView updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(80);
}];
// 删除之前所有的约束,添加新的约束
// [blueView remakeConstraints:^(MASConstraintMaker *make) {
//
// }];
// with /and
}
- (void)center
{
// 1.红色的view
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 2.添加约束
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.equalTo(@100);
// make.height.equalTo(@100);
// make.centerX.equalTo(self.view.mas_centerX);
// make.centerY.equalTo(self.view.mas_centerY);
// }];
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.mas_equalTo(100);
// make.height.mas_equalTo(100);
// make.centerX.mas_equalTo(self.view.mas_centerX);
// make.centerY.mas_equalTo(self.view.mas_centerY);
// }];
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 100));
make.center.mas_equalTo(self.view);
}];
}
- (void)edge
{
// 1.红色的view
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 2.添加约束
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.view.mas_top).offset(20);
// make.left.equalTo(self.view.mas_left).offset(20);
// make.right.equalTo(self.view.mas_right).offset(-20);
// make.bottom.equalTo(self.view.mas_bottom).offset(-20);
// }];
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.view).offset(20);
// make.left.equalTo(self.view).offset(20);
// make.right.equalTo(self.view).offset(-20);
// make.bottom.equalTo(self.view).offset(-20);
// }];
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.offset(20);
// make.left.offset(20);
// make.right.offset(-20);
// make.bottom.offset(-20);
// }];
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.left.offset(20);
// make.right.bottom.offset(-20);
// }];
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.insets(UIEdgeInsetsMake(20, 20, 20, 20));
}];
}
@end