纯代码适配
视图最基本适配方法介绍
注意:必须先把视图加到父视图再进行适配
注意:右边和底部偏移量为负数
- 距离父视图 上下左右 各10个单位
UIView *backGroudView = [[UIView alloc] init];
backGroudView.backgroundColor = [UIColor orangeColor];
[self addSubview:backGroudView];
[backGroudView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.view).offset(10);
make.left.equalTo(weakSelf.view).offset(10);
make.bottom.equalTo(weakSelf.view).offset(-10);
make.right.equalTo(weakSelf.view).offset(-10);
}];
- 视图靠左上角,宽高固定100
UIView *backGroudView = [[UIView alloc] init];
backGroudView.backgroundColor = [UIColor orangeColor];
[self addSubview:backGroudView];
[backGroudView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.view).offset(10);
make.left.equalTo(weakSelf.view).offset(10);
make.width.mas_equalTo(100);
make.height.mas_equalTo(100);
}];
- 水平线居中,宽高固定
UIView *backGroudView = [[UIView alloc] init];
backGroudView.backgroundColor = [UIColor orangeColor];
[self addSubview:backGroudView];
[backGroudView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.view).offset(10);
make.width.mas_equalTo(100);
make.height.mas_equalTo(100);
make.centerX.equalTo(weakSelf.view.mas_centerX);
}];
- 垂直线居中,宽高固定
UIView *backGroudView = [[UIView alloc] init];
backGroudView.backgroundColor = [UIColor orangeColor];
[self addSubview:backGroudView];
[backGroudView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.view).offset(10);
make.width.mas_equalTo(100);
make.height.mas_equalTo(100);
make.centerY.equalTo(weakSelf.view.mas_centerY);
}];
- 相对父视图居中,宽高固定
//方法一
UIView *backGroudView = [[UIView alloc] init];
backGroudView.backgroundColor = [UIColor orangeColor];
[self addSubview:backGroudView];
[backGroudView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(100);
make.height.mas_equalTo(100);
make.centerY.equalTo(weakSelf.view.mas_centerY);
make.centerX.equalTo(weakSelf.view.mas_centerX);
}];
//方法二
UIView *backGroudView = [[UIView alloc] init];
backGroudView.backgroundColor = [UIColor orangeColor];
[self addSubview:backGroudView];
[backGroudView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(200, 200));
make.center.equalTo(weakSelf.view);
}];