# 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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、IOS、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:数据集、包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备。
资源推荐
资源详情
资源评论
收起资源包目录
ios系统下自定义键盘,可以在键盘上更换自己的图片,支持autolayout和横竖屏切换.zip (98个子文件)
cm
ZLKeyboardDemoUITests
ZLKeyboardDemoUITests.m 1KB
Info.plist 701B
Podfile 108B
LICENSE 11KB
ZLKeyboardDemo.xcodeproj
project.pbxproj 34KB
project.xcworkspace
xcshareddata
IDEWorkspaceChecks.plist 238B
contents.xcworkspacedata 159B
Pods
Pods.xcodeproj
project.pbxproj 40KB
Manifest.lock 269B
Target Support Files
Pods-ZLKeyboardDemo
Pods-ZLKeyboardDemo-acknowledgements.plist 2KB
Pods-ZLKeyboardDemo-umbrella.h 330B
Pods-ZLKeyboardDemo.debug.xcconfig 572B
Pods-ZLKeyboardDemo-dummy.m 142B
Pods-ZLKeyboardDemo.modulemap 128B
Pods-ZLKeyboardDemo.release.xcconfig 572B
Pods-ZLKeyboardDemo-resources.sh 6KB
Pods-ZLKeyboardDemo-frameworks.sh 7KB
Pods-ZLKeyboardDemo-acknowledgements.markdown 1KB
Info.plist 828B
Masonry
Masonry.modulemap 104B
Masonry-umbrella.h 801B
Masonry-dummy.m 118B
Masonry-prefix.pch 195B
Info.plist 828B
Masonry.xcconfig 464B
Masonry
LICENSE 1KB
README.md 16KB
Masonry
MASConstraint.h 8KB
MASViewAttribute.h 1KB
NSLayoutConstraint+MASDebugAdditions.h 326B
MASConstraintMaker.h 6KB
Masonry.h 802B
NSArray+MASAdditions.m 6KB
MASLayoutConstraint.m 227B
NSLayoutConstraint+MASDebugAdditions.m 6KB
MASConstraint+Private.h 2KB
MASConstraint.m 8KB
ViewController+MASAdditions.h 891B
MASCompositeConstraint.m 5KB
View+MASShorthandAdditions.h 5KB
NSArray+MASShorthandAdditions.h 1016B
MASViewConstraint.m 13KB
View+MASAdditions.h 5KB
MASUtilities.h 6KB
MASViewAttribute.m 1KB
MASConstraintMaker.m 10KB
NSArray+MASAdditions.h 3KB
MASLayoutConstraint.h 505B
View+MASAdditions.m 6KB
MASViewConstraint.h 1KB
MASCompositeConstraint.h 494B
ViewController+MASAdditions.m 1KB
.gitignore 1KB
ZLKeyboardDemoTests
ZLKeyboardDemoTests.m 939B
Info.plist 701B
ZLKeyboardDemo
ViewController.h 234B
ViewController1.h 234B
ViewController1.m 1KB
Base.lproj
LaunchScreen.storyboard 2KB
AppDelegate.h 296B
ZLKeyboard
ZLCharKeyboard.m 11KB
ZLKeyboard.m 14KB
ZLKeyboard.h 811B
ZLNumberKeyboard.m 4KB
resource
key_icon_shift_actived.png 732B
key_normal.png 2KB
key_mood_normal.png 2KB
key_icon_shift_normal.png 29KB
key_space_pressed.png 2KB
key_num_normal.png 1KB
key_mood_pressed.png 2KB
key_space_normal.png 2KB
key_pressed.png 1KB
key_icon_del.png 2KB
key_num_pressed.png 1KB
key_icon_shift_highlighted.png 610B
ZLNumberKeyboard.h 337B
ZLKeyboardConfig.h 843B
ZLSymbolKeyboard.m 8KB
ZLCharKeyboard.h 331B
ZLSymbolKeyboard.h 337B
AppDelegate.m 2KB
main.m 353B
ViewController.m 1KB
Info.plist 1KB
Assets.xcassets
Contents.json 62B
AppIcon.appiconset
Contents.json 2KB
LaunchImage.launchimage
1125x2436.png 80KB
640x960.png 40KB
1242x2208.png 80KB
Contents.json 1KB
1136x640.png 39KB
750x1334.png 47KB
ZLKeyboardDemo.xcworkspace
xcshareddata
IDEWorkspaceChecks.plist 238B
contents.xcworkspacedata 232B
README.md 259B
Podfile.lock 269B
demo.gif 1.68MB
共 98 条
- 1
资源评论
妄北y
- 粉丝: 9669
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功