博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Masonry的使用
阅读量:4436 次
发布时间:2019-06-07

本文共 2361 字,大约阅读时间需要 7 分钟。

 

今天来介绍下Masonry 参考(http://www.cocoachina.com/ios/20141219/10702.html)

Masonry 源码:

Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS Max OS X

podfile 的引用方式

 

platform :ios, '7.0'

 

target ‘MyApp’ do

 

pod 'Masonry'

end

 

看一下Masonry支持哪一些属性

@property (nonatomic, strong, readonly) MASConstraint *left; 左侧

@property (nonatomic, strong, readonly) MASConstraint *top; 上侧

@property (nonatomic, strong, readonly) MASConstraint *right; 右侧

@property (nonatomic, strong, readonly) MASConstraint *bottom; 下侧

@property (nonatomic, strong, readonly) MASConstraint *leading; 首部

@property (nonatomic, strong, readonly) MASConstraint *trailing; 尾部

@property (nonatomic, strong, readonly) MASConstraint *width;

@property (nonatomic, strong, readonly) MASConstraint *height;

@property (nonatomic, strong, readonly) MASConstraint *centerX; 横向中点

@property (nonatomic, strong, readonly) MASConstraint *centerY; 纵向中点

@property (nonatomic, strong, readonly) MASConstraint *baseline; 文本挤线

 

 

Masonry中能够添加autolayout约束有三个函数

1

2

3

4

5

6

7

8

9

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;

- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;

- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

/*

mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错 

mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况

mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束

三种函数善加利用 就可以应对各种情况了

*/

 

 equalTo mas_equalTo的区别在哪里呢? 其实 mas_equalTo是一个MACRO

1

2

3

4

#define mas_equalTo(...)                 equalTo(MASBoxValue((__VA_ARGS__)))

#define mas_greaterThanOrEqualTo(...)    greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))

#define mas_lessThanOrEqualTo(...)       lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))

#define mas_offset(...)                  valueOffset(MASBoxValue((__VA_ARGS__)))

可以看到 mas_equalTo只是对其参数进行了一个BOX操作(装箱) MASBoxValue

Masonry的用法:

[view1.mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self).offset(20);

            make.centerX.equalTo(self);

            make.size.mas_equalTo(CGSizeMake(XCFScreenWidth-XCFRecipeCellMarginTitle*2, 50));

        }];

 

[addListButton mas_makeConstraints:^(MASConstraintMaker *make) {

                make.top.equalTo(footer).offset(20);

                make.centerX.equalTo(footer);

                make.size.mas_equalTo(CGSizeMake(120, 40));

            }];

(因为iOS中原点在左上角所以注意使用offset时注意rightbottom用负数)

offset偏移量

 

 

转载于:https://www.cnblogs.com/louisQian/p/5719891.html

你可能感兴趣的文章
复利计算器5.0
查看>>
酒厂选址
查看>>
js原生代码之图片轮播
查看>>
[NM]打开NetworkManager和wpa_supplicant的DEBUG接口
查看>>
区块链的工作流程
查看>>
深拷贝与浅拷贝的区别
查看>>
(转载)java高并发:CAS无锁原理及广泛应用
查看>>
PyInstaller打包Python脚本为exe
查看>>
Java 冒泡排序与快速排序的实现
查看>>
Linux yum安装PostgreSQL9.6
查看>>
WinPE基础知识之代码解析
查看>>
A题之拼音转数字
查看>>
C#基础知识之三
查看>>
Ural 1635 Mnemonics and Palindromes(DP)
查看>>
基于LDA对关注的微博用户进行聚类
查看>>
数据库连接错误响应码
查看>>
spring 事物不回滚
查看>>
课堂作业1
查看>>
(020)[虚拟系统]Win7网络连接红叉(无解决)
查看>>
eclipse server name 灰色
查看>>