美文网首页
10.6 Masonry-baseline约束

10.6 Masonry-baseline约束

作者: 草根小强 | 来源:发表于2019-04-26 17:29 被阅读0次

    #import "ViewController.h"
    #import "Masonry.h"
    #import "CustomView.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        CustomView *item1 = [[CustomView alloc]init];
        CustomView *item2 = [[CustomView alloc]init];
        CustomView *item3 = [[CustomView alloc]init];
        item1.backgroundColor = [UIColor redColor];
        item2.backgroundColor = [UIColor blueColor];
        item3.backgroundColor = [UIColor greenColor];
        
        [self.view addSubview:item1];
        [self.view addSubview:item2];
        [self.view addSubview:item3];
        
        item1.content = @"sdksdlaksdjklasdjklasdjklas";
        item2.content = @"asdkasdmkasdmksadkasdklasdmklasdmklas";
        item3.content = @"asdmkaslkdmsalkdklasdmsalkdmsaldmskldmkslad";
        
        item1.imageV = [UIImage imageNamed:@"dog_small"];
        item2.imageV = [UIImage imageNamed:@"dog_middle"];
        item3.imageV = [UIImage imageNamed:@"dog_big"];
        
        //创建子视图/给子视图设置约束 要在给父视图设置约束之前
        [item1 createSubViews];
        [item2 createSubViews];
        [item3 createSubViews];
        
        [item1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.top.mas_equalTo(200);
            make.width.mas_equalTo(80);
    //        make.height.mas_equalTo(100);
        }];
        
        [item2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(item1.mas_right).offset(10);
            make.baseline.mas_equalTo(item1.mas_baseline);
             make.width.mas_equalTo(80);
    //        make.height.mas_equalTo(100);
        }];
        
        [item3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(item2.mas_right).offset(10);
            make.baseline.mas_equalTo(item2.mas_baseline);
            make.width.mas_equalTo(80);
    //        make.height.mas_equalTo(100);
        }];
        
    }
    @end
    
    
    #import <UIKit/UIKit.h>
    
    @interface CustomView : UIView
    
    @property (nonatomic,copy) NSString *content;
    
    @property (nonatomic,strong) UIImage *imageV;
    
    @property (nonatomic,strong) UIView *baseLineView;
    
    
    - (void)createSubViews;
    @end
    
    #import "CustomView.h"
    #import "Masonry.h"
    
    @implementation CustomView
    
    
    - (instancetype)init{
        
        if (self = [super init]) {
            
    //        [self createSubViews];
            
        }
        return self;
    }
    
    - (void)createSubViews{
        
        UIImageView *subImageV = [[UIImageView alloc]init];
        subImageV.image = self.imageV;
        //把subImageV的底部线作为baseline
        self.baseLineView = subImageV;
        
        [self addSubview:subImageV];
        
        UILabel *label = [[UILabel alloc]init];
        label.numberOfLines = 0;
        label.text = self.content;
        [self addSubview:label];
        
        [subImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(self).offset(0);
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.bottom.mas_equalTo(self).offset(0);
            
            make.top.mas_equalTo(subImageV.mas_bottom).offset(4);
        }];
    }
    //重写
    - (UIView *)viewForBaselineLayout{
        
        return self.baseLineView;
    }
    @end
    
    Masonry-baseline约束.png

    相关文章

      网友评论

          本文标题:10.6 Masonry-baseline约束

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