正文:
前期准备:
1. 下载Masonry并导入到工程中;
2. 将Masonry.h导入当前控制器。
案例一:
要求:
无论在什么尺寸的设备上(包括横竖屏切换),红色view都居中显示。
实现:
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 防止block中的循环引用
__weak typeof(self) weakSelf = self;
// 初始化view并设置背景
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
// 使用mas_makeConstraints添加约束
[view mas_makeConstraints:^(MASConstraintMaker *make) {
// 添加大小约束(make就是要添加约束的控件view)
make.size.mas_equalTo(CGSizeMake(100, 100));
// 添加居中约束(居中方式与self相同)
make.center