Masonry使用总结

作者: 郑州程序员王一 | 来源:发表于2016-12-02 23:28 被阅读17次

以下为笔者工作总结,写法看控件设计场景,自行取舍

Masonry详解1:点击跳转

铺满上下左右
Simulator Screen Shot 2016年12月2日 下午9.51.35.png
    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
Simulator Screen Shot 2016年12月2日 下午10.19.51.png
    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);
    }];
设置位置居中,宽高
Simulator Screen Shot 2016年12月2日 下午10.18.29.png
    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));
    }];
二个视图并列
Simulator Screen Shot 2016年12月2日 下午10.35.08.png
    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);
    }];
三个视图并列
Simulator Screen Shot 2016年12月2日 下午11.16.12.png
    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]);
    }];
多个视图相隔
Simulator Screen Shot 2016年12月2日 下午11.24.18.png
    __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;
}

相关文章

  • Autolayout、VFL、Masonry

    适配 VFL语言 Masonry 代码实现Autolayout VFL代码 Masonry使用 总结 使用代码实现...

  • 实用iOS第三方框架

    界面布局 github地址:Masonry Masonry使用总结 : 赵不懂的博客 网络请求 github地址...

  • Masonry自适应UISrollview

    Masonry自适应UISrollview 总结:1.显示视图使用Masonry对srollview作约束,使to...

  • Masonry使用总结

    Masonry使用总结 一、Masonry简介 Masonry是一个轻量级的布局框架,适用于iOS以及OS X。它...

  • Masonry 自动布局 及注意点

    Masonry和FDTemplateLayoutCell搭配使用总结 http://www.jianshu.com...

  • Masonry使用总结

    核心数据结构 MASViewAttributeMASViewAttribute存储了UIView及其布局属性(NS...

  • Masonry使用总结

    https://github.com/SnapKit/Masonry https://www.cnblogs.co...

  • Masonry使用总结

    以下为笔者工作总结,写法看控件设计场景,自行取舍 Masonry详解1:点击跳转 铺满上下左右 上下左右间距为10...

  • Masonry使用总结

    在使用第三方库Masonry的过程中,遇到过各种各样的坑,以下就把笔者使用过程中的问题总结如下: 控件必须要添加到...

  • Masonry使用总结

    方法requiresConstraintBasedLayout的含义 方法+ (BOOL)requiresCons...

网友评论

    本文标题:Masonry使用总结

    本文链接:https://www.haomeiwen.com/subject/hbvqmttx.html