- (void)viewDidLoad {
[super viewDidLoad];
//防止block的循环引用
__weak typeof(self)weakself = self;
// __weak typeof(self)weakself = self;
UIView *bgview = [[UIView alloc] init];
bgview.backgroundColor = [UIColor redColor];
[self.view addSubview:bgview];
//注意: 添加约束前必须要把view添加到视图上
#if 0
//使用mas——makeConstraints添加约束
[bgview mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(weakself.view);
make.size.mas_equalTo(CGSizeMake(200, 200));
}];
#endif
#if 0
//不想固定视图的宽高,让这个视图的大小由父视图与自己的间距控制
[bgview mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(weakself.view);
make.edges.mas_offset(UIEdgeInsetsMake(10, 10, 10, 10));
}];
#endif
//单独的设置子视图与父视图某个方向的间距
//以一个子视图必定在父视图里头的思路想,top,left设定值得时候都是正数,bottom,right,都是负数
[bgview mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakself.view).with.offset(20);
make.left.equalTo(weakself.view).with.offset(10);
make.bottom.equalTo(weakself.view).with.offset(-10);
make.right.equalTo(weakself.view).with.offset(-10);
}];
//多个视图
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor greenColor];
[bgview addSubview:view1];
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(bgview);
make.size.mas_equalTo(CGSizeMake(200, 200));
make.top.equalTo(bgview).with.offset(20);//控制视图与俯视图间距
}];
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor blueColor];
[bgview addSubview:view2];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 100));
make.top.equalTo(view1.mas_bottom).with.offset(20);
make.centerX.equalTo(bgview);
// make.left.equalTo(bgview.mas_left).with.offset(100);//这一句代码,会改变视图的width,因为同时要满足,centerx,与leftoffset(20)
}];
// Do any additional setup after loading the view, typically from a nib.
}
Masory使用
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...