AutoLayout(Masonry)

Masonry使用总结

Autolayout


一、Masonry简介

Masonry是一个轻量级的布局框架,适用于iOS以及OS X。它用简洁的语法对官方的AutoLayout进行了封装。 Masonry有它自己的一套框架用来描述NSLayoutConstraints布局的DSL,提高了约束代码的简洁性与可读性。 
Masonry现处于bugfix only状态,将不再有功能性的更新。会有更多的开发者加入Swift阵营,推荐使用Swift写的Snapkit框架来布局。

snapkit


二、导入Masonry框架

  1. 使用Cocoapods来导入框架,在使用到该框架的文件中添加主头文件:#import <Masonry/Masonry.h>。 
  2. 使用直接拖拽的方式拉入框架文件夹,在使用到该框架的文件中添加主头文件:#import "Masonry.h"

三、Masonry的特性

  1. MASViewAttribute
MASViewAttribute NSLayoutAttribute
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. 简化前缀的宏定义

  • 为了增加代码的可读性这里有两个简化代码的宏:#define MAS_SHORTHAND和#define MAS_SHORTHAND_GLOBALS
  • MAS_SHORTHAND:只要在导入Masonry主头文件之前定义这个宏, 那么以后在使用Masonry框架中的属性和方法的时候, 就可以省略mas_前缀
  • MAS_SHORTHAND_GLOBALS:只要在导入Masonry主头文件之前定义这个宏,那么就可以让equalTo函数接收基本数据类型, 内部会对基本数据类型进行包装 
    注意:这两个宏如果想有效使用,必须要在添加Masonry头文件之前导入进去。在没有增加宏`MAS_SHORTHAND_GLOBALS时,下面这句是会报错的。 
    make.top.equalTo(42); --> make.top.equalTo([NSNumber numberWithInt:42]);

四、Masonry约束添加步骤

  1. 自定义UIView
  2. 将自定义的UIView添加到父视图上。
  3. 添加约束

五、Masonry的具体使用

对一个控件添加约束条件要最终满足可以确定这个空间的大小和位置,否则会报错缺少约束,当然也要避免约束冲突。下面对View1的左右上下(位置)都进行约束,并没有对其大小进行约束,但是可根据上述约束自动设置View1的大小。

1. 创建一个View,左右上下空出10个像素
 
 
  1. UIView *view1 = [[UIView alloc]init];
  2. view1.backgroundColor = [UIColor greenColor];
  3. [self.view addSubview:view1];
  4. UIEdgeInsets padding = UIEdgeInsetsMake(150, 30, 30, 30);
  5. [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
  6. make.top.equalTo(self.view.mas_top).with.offset(padding.top);
  7. make.left.equalTo(self.view.mas_left).with.offset(padding.left);
  8. make.bottom.equalTo(self.view.mas_bottom).with.offset(-padding.bottom);
  9. make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
  10. }];
  11. // 一句代码代替上面的多行
  12. // [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
  13. // make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(150, 30, 30, 30));
  14. // }];
2. 使用Priority属性来做简单的动画

.priority allows you to specify an exact priority

.priorityHigh equivalent to UILayoutPriorityDefaultHigh

.priorityMedium is half way between high and low

.priorityLow equivalent to UILayoutPriorityDefaultLow

  • 优先级约束一般放在一个控件约束的最后面,下面看示例。
 
 
  1. // 红色View
  2. UIView *redView = [[UIView alloc]init];
  3. redView.backgroundColor = [UIColor redColor];
  4. [self.view addSubview:redView];
  5. // 蓝色View
  6. self.blueView = [[UIView alloc]init];
  7. self.blueView.backgroundColor = [UIColor blueColor];
  8. [self.view addSubview:self.blueView];
  9. // 黄色View
  10. UIView *yellowView = [[UIView alloc]init];
  11. yellowView.backgroundColor = [UIColor yellowColor];
  12. [self.view addSubview:yellowView];
  13. // ---红色View--- 添加约束
  14. [redView mas_makeConstraints:^(MASConstraintMaker *make) {
  15. make.left.mas_equalTo(self.view.mas_left).with.offset(20);
  16. make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-80);
  17. make.height.equalTo([NSNumber numberWithInt:50]);
  18. }];
  19. // ---蓝色View--- 添加约束
  20. [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
  21. make.left.mas_equalTo(redView.mas_right).with.offset(40);
  22. make.bottom.width.height.mas_equalTo(redView);
  23. }];
  24. // ---黄色View--- 添加约束
  25. [yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
  26. make.left.mas_equalTo(self.blueView.mas_right).with.offset(40);
  27. make.right.mas_equalTo(self.view.mas_right).with.offset(-20);
  28. make.bottom.width.height.mas_equalTo(redView);
  29. // 优先级设置为250,最高1000(默认)
  30. make.left.mas_equalTo(redView.mas_right).with.offset(20).priority(250);
  31. }];

Masonry动画1

 
 
  1. // 点击屏幕移除蓝色View
  2. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  3. {
  4. [self.blueView removeFromSuperview];
  5. [UIView animateWithDuration:1.0 animations:^{
  6. [self.view layoutIfNeeded];
  7. }];
  8. }

Masonry动画2

解:这里的三个View的宽度是根据约束自动推断设置的,对黄色的View设置了一个与红色View有关的priority(250)的优先级,它同时有对蓝色View有个最高的优先级约束(make.left.mas_equalTo(self.blueView.mas_right).with.offset(40);)。当点击屏幕是,我将蓝色View移除,此时第二优先级就是生效。

3. Masonry官方Demo之Basic

basic1

basic2

 
 
  1. - (void)testBasic
  2. {
  3. int padding = 15;
  4. UIView *greenView = [[UIView alloc]init];
  5. [self.view addSubview:greenView];
  6. greenView.backgroundColor = [UIColor greenColor];
  7. UIView *redView = [[UIView alloc]init];
  8. [self.view addSubview:redView];
  9. redView.backgroundColor = [UIColor redColor];
  10. UIView *blueView = [[UIView alloc]init];
  11. [self.view addSubview:blueView];
  12. blueView.backgroundColor = [UIColor blueColor];
  13. // 对 绿色View 进行约束
  14. [greenView mas_makeConstraints:^(MASConstraintMaker *make) {
  15. make.left.mas_equalTo(self.view.mas_left).with.offset(padding); // X
  16. make.top.mas_equalTo(self.view.mas_top).with.offset(padding); // Y
  17. make.bottom.mas_equalTo(blueView.mas_top).with.offset(-padding);// Y --> 推断出 Height
  18. make.width.mas_equalTo(redView); // Width == 红色View(它推断出Width)
  19. }];
  20. // 对 红色View 进行约束
  21. [redView mas_makeConstraints:^(MASConstraintMaker *make) {
  22. make.left.mas_equalTo(greenView.mas_right).with.offset(padding); // X
  23. make.right.mas_equalTo(self.view.mas_right).with.offset(-padding);// X --> 推断出 Width
  24. make.bottom.and.height.mas_equalTo(greenView); // Y & Height == 绿色View(它推断出 Height&Y)
  25. }];
  26. // 对 蓝色View 进行约束
  27. [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.left.mas_equalTo(self.view.mas_left).with.offset(padding); // X
  29. make.right.mas_equalTo(self.view.mas_right).with.offset(-padding); // X --> 推断出 Width
  30. make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-padding); // Y
  31. make.height.mas_equalTo(greenView); // 注意1:Height == 绿色View(它推断出Height)
  32. }];
  33. }

总结:对绿色View约束了距离左边的距离(即确定了X值),约束了距离顶部底部蓝色View的距离(这里有个坑需要注意,很多人会认为约束了这两个就能推断出Height,其实是错误的。除非蓝色View添加一个约束让它们俩高度相同,否则即便知道了它们俩间距和分别对顶部底部的距离,也不知道红色View蓝色View高度如何。)

口诀:谁推断出大小位置(一或多),与其有约束关系的View的大小位置(一或多)向它看齐。

4. Masonry的更新约束 mas_updateConstraints

Alternatively if you are only updating the constant value of the constraint you can use the convience methodmas_updateConstraints instead of mas_makeConstraints

创建一个按钮,约束好它的位置(居中,宽高等于100且小于屏幕宽高值)。每次点击一次这个按钮,其宽高将增大一定的倍数,最终其宽高等于屏幕宽高时将不再变化。

 
 
  1. @interface ViewController ()
  2. @property (nonatomic, strong) UIButton *growingButton;
  3. @property (nonatomic, assign) CGFloat scacle;
  4. @end
 
 
  1. - (void)createGrowingButton {
  2. self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
  3. [self.growingButton setTitle:@"点我放大" forState:UIControlStateNormal];
  4. self.growingButton.layer.borderColor = UIColor.greenColor.CGColor;
  5. self.growingButton.layer.borderWidth = 3;
  6. [self.growingButton addTarget:self action:@selector(onGrowButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
  7. [self.view addSubview:self.growingButton];
  8. self.scacle = 1.0;
  9. [self.growingButton mas_makeConstraints:^(MASConstraintMaker *make) {
  10. make.center.mas_equalTo(self.view);
  11. // 初始宽、高为100,优先级最低
  12. make.width.height.mas_equalTo(100 * self.scacle);
  13. // 最大放大到整个view
  14. make.width.height.lessThanOrEqualTo(self.view);
  15. }];
  16. }
 
 
  1. - (void)onGrowButtonTaped:(UIButton *)sender {
  2. self.scacle += 1.0;
  3. // 告诉self.view约束需要更新
  4. [self.view setNeedsUpdateConstraints];
  5. // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
  6. [self.view updateConstraintsIfNeeded];
  7. [UIView animateWithDuration:0.3 animations:^{
  8. [self.view layoutIfNeeded];
  9. }];
  10. }
 
 
  1. #pragma mark - updateViewConstraints
  2. // 重写该方法来更新约束
  3. - (void)updateViewConstraints {
  4. [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
  5. // 这里写需要更新的约束,不用更新的约束将继续存在
  6. // 不会被取代,如:其宽高小于屏幕宽高不需要重新再约束
  7. make.width.height.mas_equalTo(100 * self.scacle);
  8. }];
  9. [super updateViewConstraints];
  10. }

更新约束

5. Masonry的重写约束 mas_remakeConstraints

Creates a MASConstraintMaker with the callee view. Any constraints defined are added to the view or the appropriate superview once the block has finished executing. All constraints previously installed for the view will be removed.

创建一个按钮,约束好其位置(与屏幕上左右的距离为0,与屏幕底部距离为350),点击按钮后全屏展现(即与屏幕底部距离为0)。

 
 
  1. @property (nonatomic, strong) UIButton *growingButton;
  2. @property (nonatomic, assign) BOOL isExpanded;
 
 
  1. - (void)createExpandButton {
  2. self.isExpanded = NO;
  3. self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
  4. [self.growingButton setTitle:@"点我展开" forState:UIControlStateNormal];
  5. self.growingButton.layer.borderColor = UIColor.greenColor.CGColor;
  6. self.growingButton.layer.borderWidth = 3;
  7. self.growingButton.backgroundColor = [UIColor redColor];
  8. [self.growingButton addTarget:self action:@selector(onGrowButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
  9. [self.view addSubview:self.growingButton];
  10. [self.growingButton mas_makeConstraints:^(MASConstraintMaker *make) {
  11. make.top.mas_equalTo(0);
  12. make.left.right.mas_equalTo(0);
  13. make.bottom.mas_equalTo(-350);
  14. }];
  15. }
 
 
  1. #pragma mark - updateViewConstraints
  2. - (void)updateViewConstraints {
  3. // 这里使用update也能实现效果
  4. // remake会将之前的全部移除,然后重新添加
  5. __weak __typeof(self) weakSelf = self;
  6. [self.growingButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  7. // 这里重写全部约束,之前的约束都将失效
  8. make.top.mas_equalTo(0);
  9. make.left.right.mas_equalTo(0);
  10. if (weakSelf.isExpanded) {
  11. make.bottom.mas_equalTo(0);
  12. } else {
  13. make.bottom.mas_equalTo(-350);
  14. }
  15. }];
  16. [super updateViewConstraints];
  17. }
 
 
  1. - (void)onGrowButtonTaped:(UIButton *)sender {
  2. self.isExpanded = !self.isExpanded;
  3. if (!self.isExpanded) {
  4. [self.growingButton setTitle:@"点我展开" forState:UIControlStateNormal];
  5. } else {
  6. [self.growingButton setTitle:@"点我收起" forState:UIControlStateNormal];
  7. }
  8. // 告诉self.view约束需要更新
  9. [self.view setNeedsUpdateConstraints];
  10. // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
  11. [self.view updateConstraintsIfNeeded];
  12. [UIView animateWithDuration:0.3 animations:^{
  13. [self.view layoutIfNeeded];
  14. }];
  15. }
  • mas_remakeConstraints和 mas_updateConstraints 的区别在于前者重新对视图进行了约束(抛弃了之前的约束),后者是更新约束条件(保留未更新的约束,如:这次更新了对 height 的约束,其他对X&Y以及宽的约束不变)。

重写约束1

重写约束2

6. Masonry的比例使用 multipliedBy

使用multipliedBy必须是对同一个控件本身,如果修改成相对于其它控件会出导致Crash。

 
 
  1. UIView *topView = [[UIView alloc]init];
  2. [topView setBackgroundColor:[UIColor redColor]];
  3. [self.view addSubview:topView];
  4. UIView *topInnerView = [[UIView alloc]init];
  5. [topInnerView setBackgroundColor:[UIColor greenColor]];
  6. [topView addSubview:topInnerView];
  7. UIView *bottomView = [[UIView alloc]init];
  8. [bottomView setBackgroundColor:[UIColor orangeColor]];
  9. [self.view addSubview:bottomView];
  10. UIView *bottomInnerView = [[UIView alloc]init];
  11. [bottomInnerView setBackgroundColor:[UIColor blueColor]];
  12. [bottomView addSubview:bottomInnerView];
  13. [topView mas_makeConstraints:^(MASConstraintMaker *make) {
  14. make.top.left.right.mas_equalTo(0);
  15. make.height.mas_equalTo(bottomView);
  16. }];
  17. [topInnerView mas_makeConstraints:^(MASConstraintMaker *make) {
  18. make.left.right.mas_equalTo(0);
  19. make.width.mas_equalTo(topInnerView.mas_height).multipliedBy(3);
  20. make.center.mas_equalTo(topView);
  21. }];
  22. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  23. make.left.bottom.right.mas_equalTo(0);
  24. make.height.mas_equalTo(topView);
  25. make.top.mas_equalTo(topView.mas_bottom);
  26. }];
  27. [bottomInnerView mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.top.bottom.mas_equalTo(bottomView);
  29. make.height.mas_equalTo(bottomInnerView.mas_width).multipliedBy(3);
  30. make.center.mas_equalTo(bottomView);
  31. }];

multiply

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值