以下为笔者工作总结,写法看控件设计场景,自行取舍
Masonry详解1:点击跳转
铺满上下左右
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left);
make.right.equalTo(self.view.mas_right);
make.top.equalTo(self.view.mas_top);
make.bottom.equalTo(self.view.mas_bottom);
}];
上下左右间距为10
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
UIEdgeInsets padding = UIEdgeInsetsMake(50, 50, 50, 50);
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).with.insets(padding);
}];
设置位置居中,宽高
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
// 设置当前center和父视图的center一样
make.center.mas_equalTo(self.view);
// 设置当前视图的大小
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
二个视图并列
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view2];
int padding = 10;
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view.mas_centerY);
make.left.equalTo(self.view.mas_left).with.offset(padding);
make.right.equalTo(view2.mas_left).with.offset(-padding);
make.height.mas_equalTo(@120);
make.width.equalTo(view2);
}];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view.mas_centerY);
make.left.equalTo(view1.mas_right).with.offset(padding);
make.right.equalTo(self.view.mas_right).with.offset(-padding);
make.height.mas_equalTo(view1);
make.width.equalTo(view1);
}];
三个视图并列
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor greenColor];
[self.view addSubview:view2];
UIView *view3 = [[UIView alloc] init];
view3.backgroundColor = [UIColor blueColor];
[self.view addSubview:view3];
int padding = 10;
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view);
make.left.equalTo(self.view).with.offset(padding);
make.right.equalTo(view2.mas_left).with.offset(-padding);
make.height.mas_equalTo(@150);
make.width.equalTo(@[view2, view3]);
}];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view);
make.height.mas_equalTo(view1);
make.width.equalTo(@[view1, view3]);
}];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view);
make.left.equalTo(view2.mas_right).with.offset(padding);
make.right.equalTo(self.view).with.offset(-padding);
make.height.mas_equalTo(view1);
make.width.equalTo(@[view2, view1]);
}];
多个视图相隔
__weak typeof(self) weakSelf = self;
UIView * tempView = [[UIView alloc]init];
NSInteger count = 5;//设置一排view的个数
NSInteger margin = 10;//设置相隔距离
NSInteger height = 50;//设置view的高度
for (int i = 0; i < count; i ++) {
UIView * view = [[UIView alloc]init];
view.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
[self.view addSubview:view];
if (i == 0) {
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.view).offset(margin);
make.centerY.equalTo(weakSelf.view);
make.height.mas_equalTo(height);
}];
}else if (i == (count - 1)){
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(weakSelf.view).offset(-margin);
make.left.equalTo(tempView.mas_right).offset(margin);
make.centerY.equalTo(tempView);
make.height.equalTo(tempView);
make.width.equalTo(tempView);
}];
}else{
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(tempView.mas_right).offset(margin);
make.centerY.equalTo(tempView);
make.height.equalTo(tempView);
make.width.equalTo(tempView);
}];
}
tempView = view;
[view layoutIfNeeded];
}
关于页面中的 地址、备注等 按服务器返回文字,自适应高度的解决方法
场景:服务器返回备注信息过长
cell中的设置
//订单备注
self.orderRemarks = [[UILabel alloc]init];
self.orderRemarks.textColor = WYRGBColor(45, 45, 45);
self.orderRemarks.font = [UIFont systemFontOfSize:15];
self.orderRemarks.numberOfLines = 0;
[self.contentView addSubview:self.orderRemarks];
//订单备注
[self.orderRemarks mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.orderRemarksText.mas_right).offset(10);
make.top.equalTo(self.fiveLine.mas_bottom).offset(10);
make.width.mas_equalTo(WYScreenW - 91);
}];
model中的设置
//.h中声明
/** 订单信息cell-备注高度 */
@property(nonatomic,assign)CGFloat OrderRemarkCellHeight;
//.m中实现
//订单信息cell-备注高度
-(CGFloat)OrderRemarkCellHeight{
//如果为空 再去加载
if (!_OrderRemarkCellHeight) {
//文字的最大尺寸
CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 91, MAXFLOAT);
// 计算文字的高度
CGFloat textH = [self.remark boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
//18为文字字体大小的高度,自行通过视图查看文字高度多少
_OrderRemarkCellHeight = textH - 18;
}
return _OrderRemarkCellHeight;
}
控制器中计算高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.detailedModel.OrderRemarkCellHeight;
}