1,原理
UILable 不需要定义底部距离,自定义适配 ,让UIView的底部等于UILable的底部距离多少,别忘了设置UILable的属性
代码如下:
定义一个UIView 和 UILable
#pragma mark 懒加载
-(UIView *)backView{
if(!_backView){
_backView = [[UIView alloc]init];
_backView.backgroundColor = White_Color;
_backView.layer.cornerRadius = CGFloatBasedI375(10.0f);
_backView.layer.masksToBounds = YES;
[self addSubview:self.backView];
}
return _backView;
}
-(UILabel *)noticeLabel{
if(!_noticeLabel){
_noticeLabel = [UILabel labelWithText:@"非常抱歉,该车位已被占用,升起车位锁失败,订单自动取消" atColor:RGB(51,51,51) atTextSize:CGFloatBasedI375(16) atTextFontForType:Common_Font];
[self ajusttheWidth:_noticeLabel];//设置UILabel的属性
[self.backView addSubview:self.noticeLabel];
}
return _noticeLabel;
}
//UILabel属性设置 必须写 否则无法适配
-(void)ajusttheWidth:(UILabel *)label{
label.preferredMaxLayoutWidth = (SCREEN_WIDTH -CGFloatBasedI375(20));
[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
label.numberOfLines = 0;
[label sizeToFit];
}
//布局
-(void)setlayout{
[UILable mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(CGFloatBasedI375(20)); //左边距离
make.right.offset(CGFloatBasedI375(-20)); //右边距离
make.height.greaterThanOrEqualTo(@20);
make.top.equalTo(weakself.stutasLabel.mas_bottom).offset(CGFloatBasedI375(6)); //上边距离
}];
[UIView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(CGFloatBasedI375(20));//左边距离
make.right.offset(CGFloatBasedI375(-20));//右边距离
make.centerY.mas_equalTo(weakself.mas_centerY);//垂直居中
make.bottom.equalTo(weakself.UILable.mas_bottom).offset(CGFloatBasedI375(10));//重点是这句 view的底部是UILable底部距离多少
}];
}
//获取屏幕 宽度
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
UIKIT_STATIC_INLINE CGFloat CGFloatBasedI375(CGFloat size) {
// 对齐到2倍数
return trunc(size * UIScreen.mainScreen.bounds.size.width / 375 * 2) / 2.0;
} 针对苹果6的尺寸进行各手机版本适配