Masonry
2016-10-31 本文已影响44人
coder_hong
#import "ViewController.h"
//define this constant if you want to use Masonry without the 'mas_' prefix
// 只要在导入Masonry主头文件之前定义这个宏, 那么以后在使用Masonry框架中的属性和方法的时候, 就可以省略mas_前缀
// 如果这个宏是在导入了Masonry.h之后定义, 那么无效
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
// 只要在导入Masonry主头文件之前定义这个宏,那么就可以让equalTo函数接收基本数据类型, 内部会对基本数据类型进行包装
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
@interface ViewController ()
/**
*/
@property (nonatomic,weak)UIView *redView;
/**
* <#注释#>
*/
@property (nonatomic,weak)UIView *blueView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.创建控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
// 2.将控件添加到父控件中
[self.view addSubview:redView];
self.redView = redView;
[self.view addSubview:blueView];
self.blueView = blueView;
// 3.禁用Autoresizing
redView.translatesAutoresizingMaskIntoConstraints = NO;
blueView.translatesAutoresizingMaskIntoConstraints = NO;
// 4.添加约束
// 4.1设置蓝色的约束
[blueView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.top).offset(20); // y
make.left.equalTo(self.view.left).offset(20); // x
make.right.equalTo(self.view.right).offset(-20); // w
make.height.equalTo(50);
}];
// 4.2设置红色的约束
[redView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(blueView.bottom).offset(20);
make.right.equalTo(blueView.right);
make.height.equalTo(blueView.height);
make.width.equalTo(blueView.width).multipliedBy(0.5);
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// makeConstraints: 每次都会添加新的约束
/*
[self.redView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.blueView.bottom).offset(100);
}];
*/
// updateConstraints: 专门用于更新约束的, 如果没有约束会创建一个新的 如果有直接修改以前的
/*
[self.redView updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.blueView.bottom).offset(100);
}];
*/
// remakeConstraints: 清空约束, 删除约束
[self.redView remakeConstraints:^(MASConstraintMaker *make) {
}];
}
- (void)demo2
{
// 1.创建控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 2.将控件添加到父控件中
[self.view addSubview:redView];
// 3.禁用Autoresizing
redView.translatesAutoresizingMaskIntoConstraints = NO;
// 4.添加约束
/*
// and / with masonry中的这两个属性没有任何含义, 仅仅用于提高阅读性
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).with.offset(20);
make.left.equalTo(self.view.mas_left).offset(20);
// make.width.equalTo(@100);
// make.height.equalTo(@100);
// make.width.height.equalTo(@100);
make.width.and.height.equalTo(@100);
}];
*/
// 去掉mas_前缀
//[redView mas_makeConstraints:^(MASConstraintMaker *make) {
[redView makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.view.mas_top).with.offset(20);
make.top.equalTo(self.view.top).with.offset(20);
make.left.equalTo(self.view.mas_left).offset(20);
// make.width.and.height.equalTo(@100);
// make.width.and.height.mas_equalTo(100);
make.width.and.height.equalTo(100);
}];
}
- (void)demo
{
// 1.创建控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 2.将控件添加到父控件中
[self.view addSubview:redView];
// 3.禁用Autoresizing
redView.translatesAutoresizingMaskIntoConstraints = NO;
// 4.添加约束
/*
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).offset(20);
make.left.equalTo(self.view.mas_left).offset(20);
make.bottom.equalTo(self.view.mas_bottom).offset(-20);
make.right.equalTo(self.view.mas_right).offset(-20);
}];
*/
/*
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(20);
make.left.equalTo(self.view).offset(20);
make.bottom.equalTo(self.view).offset(-20);
make.right.equalTo(self.view).offset(-20);
}];
*/
/*
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(20);
make.left.offset(20);
make.bottom.offset(-20);
make.right.offset(-20);
}];
*/
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.insets(UIEdgeInsetsMake(20, 20, 20, 20));
}];
}
@end