【iOS】Masonry初步认识

引入Masonry

cocoapods

要引入Masonry,首先要保证你的Mac上已经安装了cocoapods且能正常运作,下面推荐两篇博客:
安装cocoapods针对M1芯片安装cocoapods

下载Masonry

首先检查源是否存在:pod search masonry

然后进行接下来的操作:

  1. 打开终端,cd到所需要下载的文件目录:cd /文件目录(也可以打开访达,找到想要的文件,拖进来)

  2. touch PodFile(在刚才指定的目录下创建了一个Podfile空文件)请添加图片描述

  3. 打开这个文件,将以下内容输入进去,并保存

platform :ios, ‘8.0’
target '你的项目名称’ do
pod ‘Masonry’
end

在这里插入图片描述

  1. 回到终端,输入 pod install

等待一段时间后,就会出现

Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

绿色字样,代表安装完成,此时再打开项目工程的文件夹,会发现多了几个文件:
请添加图片描述

请添加图片描述
之后要打开项目,就点击后缀名为xcworkspace的项目进入。

Masonry简单使用

.h文件:


#import <UIKit/UIKit.h>
//在此处引入Masonry的头文件
#import "Masonry.h"
@interface ViewController : UIViewController


@end

.m文件

#import "ViewController.h"

@interface ViewController ()

@end

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

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

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

    }];

     其基础的设置属性如下:

     1.尺寸:width、height、size。
     2.边界:left、leading、right、trailing、top、bottom、edges。
     3.中心点:center、centerX、centerY。
     4.偏移量:offset、insets、sizeOffset、centerOffset。
     5.priority()约束优先级(0~1000),multipler乘因数,dividedBy除因数。

     大小关系有三种:

     1.equalTo,相等。
     2.lessThanOrEqualTo,小于等于。
     3.greaterThanOrEqualTo,大于等于。

*/






- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView *firstView = [[UIView alloc] init];
    firstView.backgroundColor = [UIColor orangeColor];
    
    UIView *secondView = [[UIView alloc] init];
    secondView.backgroundColor = [UIColor greenColor];
    
    UIView *thirdView = [[UIView alloc] init];
    thirdView.backgroundColor = [UIColor cyanColor];
    
    
    [self.view addSubview:firstView];
    [firstView addSubview:secondView];
    [secondView addSubview:thirdView];

    [firstView mas_makeConstraints:^(MASConstraintMaker* make) {
        //make.left.mas_equalTo(0); //直接在equalTo()内填写数字,默认为相对于父视图而言,等同于下面注释的语句
        make.left.mas_equalTo(self.view.mas_left).mas_offset(100);
        make.top.mas_equalTo(self.view.mas_top).mas_offset(60);
        make.width.mas_equalTo(self.view).dividedBy(2);
        make.height.mas_equalTo(self.view).dividedBy(5);
    }];
    
    [secondView mas_makeConstraints:^(MASConstraintMaker* make) {
        //相对于firstView左边界40距离
        make.left.mas_offset(40);
        //相对于firstView左边界40距离
        make.top.mas_offset(40);
        make.width.mas_equalTo(self.view).dividedBy(2);
        make.height.mas_equalTo(self.view).dividedBy(5);
    }];
    
    [thirdView mas_makeConstraints:^(MASConstraintMaker* make) {
        //相对于secondView左边界40距离
        make.left.mas_offset(40);
        //相对于secondView上边界40距离
        make.top.mas_offset(40);
        make.width.mas_equalTo(self.view).dividedBy(2);
        make.height.mas_equalTo(self.view).dividedBy(5);
    }];


    
}


@end

请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值