#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
Masonry
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 纯手写代码一共经历了三个时期,这里我就不介绍了,随着iPhone6的出现,苹果手机的尺寸也就不在单一。为了让...
- ] *** Assertion failure in -[MASViewConstraint addConstra...
- 用cocoapods导入了Masonry在.pch文件中引入时却出现了下面的问题 This happened to...