# Masonry [![Build Status](https://travis-ci.org/SnapKit/Masonry.svg?branch=master)](https://travis-ci.org/SnapKit/Masonry) [![Coverage Status](https://img.shields.io/coveralls/SnapKit/Masonry.svg?style=flat-square)](https://coveralls.io/r/SnapKit/Masonry) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) ![Pod Version](https://img.shields.io/cocoapods/v/Masonry.svg?style=flat)
**Masonry is still actively maintained, we are committed to fixing bugs and merging good quality PRs from the wider community. However if you're using Swift in your project, we recommend using [SnapKit](https://github.com/SnapKit/SnapKit) as it provides better type safety with a simpler API.**
Masonry is a light-weight layout framework which wraps AutoLayout with a nicer syntax. Masonry has its own layout DSL which provides a chainable way of describing your NSLayoutConstraints which results in layout code that is more concise and readable.
Masonry supports iOS and Mac OS X.
For examples take a look at the **Masonry iOS Examples** project in the Masonry workspace. You will need to run `pod install` after downloading.
## What's wrong with NSLayoutConstraints?
Under the hood Auto Layout is a powerful and flexible way of organising and laying out your views. However creating constraints from code is verbose and not very descriptive.
Imagine a simple example in which you want to have a view fill its superview but inset by 10 pixels on every side
```obj-c
UIView *superview = self.view;
UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[superview addConstraints:@[
//view1 constraints
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:padding.top],
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:padding.left],
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-padding.bottom],
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeRight
multiplier:1
constant:-padding.right],
]];
```
Even with such a simple example the code needed is quite verbose and quickly becomes unreadable when you have more than 2 or 3 views.
Another option is to use Visual Format Language (VFL), which is a bit less long winded.
However the ASCII type syntax has its own pitfalls and its also a bit harder to animate as `NSLayoutConstraint constraintsWithVisualFormat:` returns an array.
## Prepare to meet your Maker!
Heres the same constraints created using MASConstraintMaker
```obj-c
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
make.left.equalTo(superview.mas_left).with.offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];
```
Or even shorter
```obj-c
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(superview).with.insets(padding);
}];
```
Also note in the first example we had to add the constraints to the superview `[superview addConstraints:...`.
Masonry however will automagically add constraints to the appropriate view.
Masonry will also call `view1.translatesAutoresizingMaskIntoConstraints = NO;` for you.
## Not all things are created equal
> `.equalTo` equivalent to **NSLayoutRelationEqual**
> `.lessThanOrEqualTo` equivalent to **NSLayoutRelationLessThanOrEqual**
> `.greaterThanOrEqualTo` equivalent to **NSLayoutRelationGreaterThanOrEqual**
These three equality constraints accept one argument which can be any of the following:
#### 1. MASViewAttribute
```obj-c
make.centerX.lessThanOrEqualTo(view2.mas_left);
```
MASViewAttribute | NSLayoutAttribute
------------------------- | --------------------------
view.mas_left | NSLayoutAttributeLeft
view.mas_right | NSLayoutAttributeRight
view.mas_top | NSLayoutAttributeTop
view.mas_bottom | NSLayoutAttributeBottom
view.mas_leading | NSLayoutAttributeLeading
view.mas_trailing | NSLayoutAttributeTrailing
view.mas_width | NSLayoutAttributeWidth
view.mas_height | NSLayoutAttributeHeight
view.mas_centerX | NSLayoutAttributeCenterX
view.mas_centerY | NSLayoutAttributeCenterY
view.mas_baseline | NSLayoutAttributeBaseline
#### 2. UIView/NSView
if you want view.left to be greater than or equal to label.left :
```obj-c
//these two constraints are exactly the same
make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);
```
#### 3. NSNumber
Auto Layout allows width and height to be set to constant values.
if you want to set view to have a minimum and maximum width you could pass a number to the equality blocks:
```obj-c
//width >= 200 && width <= 400
make.width.greaterThanOrEqualTo(@200);
make.width.lessThanOrEqualTo(@400)
```
However Auto Layout does not allow alignment attributes such as left, right, centerY etc to be set to constant values.
So if you pass a NSNumber for these attributes Masonry will turn these into constraints relative to the view’s superview ie:
```obj-c
//creates view.left = view.superview.left + 10
make.left.lessThanOrEqualTo(@10)
```
Instead of using NSNumber, you can use primitives and structs to build your constraints, like so:
```obj-c
make.top.mas_equalTo(42);
make.height.mas_equalTo(20);
make.size.mas_equalTo(CGSizeMake(50, 100));
make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));
```
By default, macros which support [autoboxing](https://en.wikipedia.org/wiki/Autoboxing#Autoboxing) are prefixed with `mas_`. Unprefixed versions are available by defining `MAS_SHORTHAND_GLOBALS` before importing Masonry.
#### 4. NSArray
An array of a mixture of any of the previous types
```obj-c
make.height.equalTo(@[view1.mas_height, view2.mas_height]);
make.height.equalTo(@[view1, view2]);
make.left.equalTo(@[view1, @100, view3.right]);
````
## Learn to prioritize
> `.priority` allows you to specify an exact priority
> `.priorityHigh` equivalent to **UILayoutPriorityDefaultHigh**
> `.priorityMedium` is half way between high and low
> `.priorityLow` equivalent to **UILayoutPriorityDefaultLow**
Priorities are can be tacked on to the end of a constraint chain like so:
```obj-c
make.left.greaterThanO
没有合适的资源?快使用搜索试试~ 我知道了~
iOS组件化简单demo.zip
共548个文件
h:57个
m:41个
plist:27个
1星 需积分: 16 3 下载量 169 浏览量
2020-11-11
15:49:39
上传
评论
收藏 2.08MB ZIP 举报
温馨提示
iOS组件化简单demo 包含两个业务组件、一个中间组件、主工程 + (void)registClass:(Class)class forProtocol:(Protocol *)protocol { [[CSModuleManager defaultManager] registClass:class forProtocol:protocol]; } + (nullable id)instanceForProtocol:(Protocol *)protocol { return [[CSModuleManager defaultManager] instanceForP
资源详情
资源评论
资源推荐
收起资源包目录
iOS组件化简单demo.zip (548个子文件)
002beb4c117480a01793b64db6ca8368a0fa7c 1012B
01c930004221b0b271301be2fe4703ead5a092 826B
01c930004221b0b271301be2fe4703ead5a092 826B
01c930004221b0b271301be2fe4703ead5a092 826B
02934d9e8adf2dd34b124eff617f61dc82440a 140B
031628ab1f4a7d330b053737635ca147e9fec6 921B
05063b97c4c491429d7b00373944bc5635737d 261B
06771e8a7c0c08718e8e277b967e54c45be4f2 261B
06f1b3df5c4f2f4c216db920327c85dc47dfa0 517B
087f0fef80627e595398a32dca5e04f8d01614 125B
087f0fef80627e595398a32dca5e04f8d01614 125B
09678db3aa1b8ab783a4288ecd96a8b6923322 272B
098dfd540c3301430809267eb38448327122a9 809B
0a3891c99d48eea300520b1abb2cde32868c32 302B
0d54df05905345b96ba63f9c8848a4a3f90f6b 714B
0fd5db31f260630b51c8b71ba486b05c87d8c0 2KB
123d283599c8d608fd568c75019614e7324666 529B
12dc0b3b57d45777f94fb4104f13d7cb8c9b67 891B
19dcc0ea9da85a0d964fd422122b02e55b1cdc 146B
1cc2c87e0b1596d815776d5105a0ebb2c655f7 151B
1d32e09098fbc50158db598fb2590fe6511ca5 222B
1dadc887bf35cb4d6677f4f200306c91d076b9 4KB
1de60bc99928f1ec83bf4e924f0292d5aea4e8 558B
1f5df8c640a9676221350eeafec86ec908dbae 343B
1f623cc306ee027e64e0492fd8a269a5ac865e 188B
20336a8d4f440126030493b97f2cb3e759786f 206B
21d31d3606776c01822ecabf0c568e9e8647f4 920B
222ab4ea2b3db06fa7e1df38874f4ff9cb5327 219B
22551a37c3f033daaf55e5d580d08bcdb5f5b1 176B
235be9802c4edef2df87052f9dcce28a37cc63 840B
23f881057a7407b8f884befc78616d726ddf40 276B
24b87aed130d0e8c13e5a07698fdc0926e1b1f 79B
25372cbddaa99072c525ceba414680bf89a551 240B
25f8e3305efa07fa9cab4fbc804c3878a451a2 153B
261732ea6ce2d7eac4be4f6b694bef459b4841 303B
26f39be116eb8abbfe592118022f396cda48a7 176B
2795338f7aa000b664a5754e9051be5849efe3 159B
2795338f7aa000b664a5754e9051be5849efe3 159B
2804868c00a44ca3d675c09073549954fdc22a 894B
299dcb4bdaa913a29a9f414dd04b6b452707e4 681B
2b459ab4fcd0dd41b9b6a4c4beed7ca7628e82 177B
2c62786a9f67d76d9db04d99096283949a6a09 52B
2d66b14345a1614074376303114be1d7adba8a 893B
2eb9caaece1c681e2a7e3909e100f008b10931 896B
2ee1c3aa39c2da4bf3520f0c8fa9020bd77ad1 1KB
348223cdb1b95c0515a9f8687754b72ff3447a 262B
34d9c3dafc3340a46b1ccb41afc9b9fe51eac7 843B
34d9c3dafc3340a46b1ccb41afc9b9fe51eac7 843B
34d9c3dafc3340a46b1ccb41afc9b9fe51eac7 843B
35d857fd46f7c1ce5a04cfbbeb753653d29b76 838B
35e811069215b94e3253d1fd08e24521ac96b2 522B
35fb685bb15c8ecaf3cffe91ce49a9fee753fb 1009B
37597b848a1890bb30ba0ff4102f8107cafdcb 61B
37597b848a1890bb30ba0ff4102f8107cafdcb 61B
37597b848a1890bb30ba0ff4102f8107cafdcb 61B
38b21132ecfcc16b36b39187f5831a5966fd62 348B
39d1232875537682fd94a40b4c9d688380f245 139B
3b27612920194fcbdb3cc6f468d564a4b5cf44 151B
3c3537353f304ec6a868f2f17ad054c36a94fd 105B
3cc9b8ba4e4ae5c6df5ed5e3f199668b7fead0 68B
3cc9b8ba4e4ae5c6df5ed5e3f199668b7fead0 68B
3cc9b8ba4e4ae5c6df5ed5e3f199668b7fead0 68B
3d6aaf84e2fcd969ca3544f40385b9e2a03fd0 153B
3d6cb138b1291ef0cefc0928bf2e4e6ba3c974 203B
3ec4f544bd60e28f64bcebee1c298e8fc6f44a 265B
403136c0d4946287d221544eeab43175d8e735 513B
404f5fb0e92b9c28adb35dfbd2322e672ea653 176B
40e60115e2d6553d4d1a4af1a0f3074152b57a 4KB
42bed0cae32c1e3ca4e3f6d65f874cb7e303eb 885B
42ec24d24ea8d8dc7b93288007da03f8ae9b29 259B
43903febbd8dae8c8691c2a6608376e65c883c 521B
45e9f52a72adef705bb34355d799a580e715fd 159B
4657ac86429855acc06b3541c4a873b211e41e 78B
476814291f3b0cd8dcc9e31230342956900a35 1KB
47d10719525a7dfcddb36032f4ebf104d81ff0 375KB
4a25692001452f977c7902452963d8dba72fbd 546B
4a493ca27869f145624ab9f03a779245d97851 512B
4c2024028e11d16dba01e1d49b5b1246c79d8e 447B
4d188d8fffc5aa696fc5f6d7fa32ec079c23d3 261B
4ec2aa3b801ada2e9f6e0ffec3ae7751e079b6 545B
5270467c23419a484b94b9db4f00399ba8b20d 78B
528b46b9a16945a88485eb0e4bf2adf36121ec 126B
55309e8035163b7315ead2d3ce7c6513dbfc3f 434B
57159b5134d1f930410fd0f494f1dbaf49e414 1020B
576eec2d1a9a95dd723bc5d2c8baa1b87f4e4a 865B
587300ea3e96e1717e1fd5de703b0ba1a0696d 51B
5a8e715ad5e81a0fba3ef0190e77a68be83f4b 39B
5a8e715ad5e81a0fba3ef0190e77a68be83f4b 39B
5a8e715ad5e81a0fba3ef0190e77a68be83f4b 39B
5bfed63735bf2cdc52451ea4b2b696b9425f44 637B
5e78963931ed7167027d872ec3e89bcda4ceab 105B
5e8b1f412e0df4ef3097955ba7d1debe68c444 704B
5eb28d44694d97859b91beb7857fbdbca67a47 285B
5fe5a95f1f2603ca4ca8bd059661f64e160b74 79B
6116b96ff2140cda3c766f3a0239b4156c906b 78B
645f8d27962a8072df85c7a71b93300e457a77 210B
64d0bc3dd917926892c55e3706cc116d5b165e 53B
64d0bc3dd917926892c55e3706cc116d5b165e 53B
64d0bc3dd917926892c55e3706cc116d5b165e 53B
65e5883f83cf09b5d2e9b588c7f0ae63eefb13 134B
共 548 条
- 1
- 2
- 3
- 4
- 5
- 6
hcsaaron
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1