1.
#import "ViewController.h"
//只要添加了这个宏,就不用带 mas_前缀
#define MAS_SHORTHAND
//只要添加了这个宏,equalTo就等价于mas_equalTo
#define MAS_SHORTHAND_GLOBALS
//这个头文件一定要放在前面2个宏的后面
#import "Masonry.h"2.- (void)viewDidLoad {
[super viewDidLoad];
// 1.创建控件
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 2.添加约束
CGFloat margin = 20;
CGFloat height = 50;
[blueView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left).offset(margin);
make.right.equalTo(redView.left).offset(-margin);
make.bottom.equalTo(self.view.bottom).offset(-margin);
make.height.equalTo(height);
make.top.equalTo(redView.top);
make.bottom.equalTo(redView.bottom);
make.width.equalTo(redView.width);
}];
[redView makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view.right).offset(-margin);
}];
}
本文介绍如何使用Masonry库为iOS应用中的UIView设置自动布局约束。通过具体的代码示例,展示了两个UIView之间的布局关系定义,包括左右对齐、高度设置及上下间距等。
1258

被折叠的 条评论
为什么被折叠?



