美文网首页
10.3 Masonry多视图约束

10.3 Masonry多视图约束

作者: 草根小强 | 来源:发表于2019-04-25 21:00 被阅读0次

    Masonry多视图约束

    #import "ViewController.h"
    #import "Masonry.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIButton *redBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        redBtn.backgroundColor = [UIColor redColor];
        [redBtn setTitle:@"按钮1" forState:UIControlStateNormal];
        [self.view addSubview:redBtn];
        
        
        UIButton *blueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        blueBtn.backgroundColor = [UIColor blueColor];
        [blueBtn setTitle:@"按钮2" forState:UIControlStateNormal];
        [self.view addSubview:blueBtn];
        
        UIButton *greenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [greenBtn setTitle:@"按钮3" forState:UIControlStateNormal];
        greenBtn.backgroundColor = [UIColor greenColor];
        
        [self.view addSubview:greenBtn];
        
        //使用Masonry不需要设置这个属性
        greenBtn.translatesAutoresizingMaskIntoConstraints = NO;
        
    //    [redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    //        //
    //    }];
        
    //    [@[redBtn,blueBtn,greenBtn] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:90 leadSpacing:10 tailSpacing:10];
        //MASAxisTypeHorizontal水平方向  三个按钮在同一水平线上
        //宽、左、右 固定的
        
        
         
        [@[redBtn,blueBtn,greenBtn] mas_makeConstraints:^(MASConstraintMaker *make) {
            //统一设置按钮的top的约束
            make.top.mas_equalTo(100);
            make.height.mas_equalTo(50);
        }];
        
        [@[redBtn,blueBtn,greenBtn] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
        //水平方向 按钮的间距是固定的
        //leading和tailling 固定的
    }
    @end
    
    Masonry多视图约束.png

    相关文章

      网友评论

          本文标题:10.3 Masonry多视图约束

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