iOS—— masonry的基本使用

iOS——masonry的基本使用

首先,我们必须知道使用masonry的好处,因为我们使用的模拟机尺寸可能不一样,所以使用masonry进行布局可以使视图在不同的模拟机上的位置基本保持相同,但是使用Masonry要先配置CocoaPods。

用法

	//这个方法只会添加新的约束
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

    }];
    //这个将会将以前的约束删掉,添加新的
    [blueView mas_remakeConstraints:^(MASConstraintMaker *make) {

    }];
    //这个方法将会覆盖以前的某些特定的约束
    [blueView mas_updateConstraints:^(MASConstraintMaker *make) {

    }];
make.top.mas_equalTo(x);//顶部等于x
make.top.mas_offset(x);//顶部距离父视图的mas_top有+x个单位,规定向下为正

事例

#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *leftView = [[UIView alloc] init];
        [self.view addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(100);
            make.left.mas_equalTo(self.view.mas_left);
            make.width.mas_offset(50);
            make.height.mas_equalTo(50);
           
        }];
        leftView.backgroundColor = [UIColor blueColor];
    
    UIView *rightView = [[UIView alloc] init];
    [self.view addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(100);
        make.left.mas_equalTo(340);
        make.width.mas_offset(50);
        make.height.mas_equalTo(50);
    }];
    rightView.backgroundColor = [UIColor orangeColor];
    
    UIView *centerView = [[UIView alloc] init];
       [self.view addSubview:centerView];
       [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
  
           make.center.equalTo(self.view);
           make.centerX.equalTo(self.view.mas_centerX);
           make.centerY.equalTo(self.view.mas_centerY);
           make.width.and.height.mas_equalTo(@100);
       }];
       centerView.backgroundColor = [UIColor greenColor];
  
}

你可能感兴趣的:(ios)