Masonry 多个控件等宽高布局设置方法 iOS

2016-06-22  本文已影响6393人  骑行怪状
生活的屁 底部的三个控件等高等宽,位置均匀

实现思路:

Masonry 里有两个这样的属性

centerX   NSLayoutAttributeCenterX    横向中点
centerY   NSLayoutAttributeCenterY    纵向中点

我们通过上边的这两个属性帮助我们实现


切换横竖屏我们看一下效果

横屏效果

代码如下:



    __weak typeof(self) weakSelf = self;
    
    self.viewForBottom = [[UIImageView alloc]init];
    [self.view addSubview:self.viewForBottom];
    self.viewForBottom.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
    [self.viewForBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(weakSelf.view).offset(0);
        make.left.equalTo(weakSelf.view).offset(0);
        make.right.equalTo(weakSelf.view).offset(0);
        make.height.equalTo(weakSelf.view.mas_height).multipliedBy(1/13.0f);
        
    }];
    
    
    
    self.cropButton = [[UIButton alloc]init];
    self.previewButton = [[UIButton alloc]init];
    self.saveButton = [[UIButton alloc]init];
    
    
    [self.viewForBottom addSubview:self.saveButton];
    [self.viewForBottom addSubview:self.cropButton];
    [self.viewForBottom addSubview:self.previewButton];
    
    
    self.cropButton.backgroundColor = [UIColor colorWithRed:0.8 green:1.0 blue:0.4 alpha:1.0];
    self.saveButton.backgroundColor = [UIColor colorWithRed:1.0 green:0.8 blue:0.4 alpha:1.0];
    self.previewButton.backgroundColor = [UIColor yellowColor];
    
    
    [self.cropButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(weakSelf.viewForBottom).offset(40);
        //中心 Y
        make.centerY.equalTo(weakSelf.viewForBottom);
        make.height.equalTo(weakSelf.cropButton.mas_width);
        make.height.equalTo(weakSelf.view.mas_height).multipliedBy(1/25.0f);

    }];
    
    
    
    [self.previewButton mas_makeConstraints:^(MASConstraintMaker *make) {

        //中心 Y
        make.centerX.equalTo(weakSelf.viewForBottom);
        make.centerY.equalTo(weakSelf.viewForBottom);
        make.height.equalTo(weakSelf.previewButton.mas_width);
        make.height.equalTo(weakSelf.view.mas_height).multipliedBy(1/25.0f);

    }];
    
    
    
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(weakSelf.viewForBottom).offset(-40);
        make.centerY.equalTo(weakSelf.viewForBottom);
        make.height.equalTo(weakSelf.saveButton.mas_width);
        make.height.equalTo(weakSelf.view.mas_height).multipliedBy(1/25.0f);
        
    }];
    

    
    

学习:
里脊串 Masonry介绍与使用实践(快速上手Autolayout)

上一篇 下一篇

猜你喜欢

热点阅读