Masonry 控件详解

作者: 船长One | 来源:发表于2016-09-19 09:40 被阅读1069次

Masonry 控件详解

1.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; //文本基线

2.Masonry给我们提供了3个方法

//新增约束

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

//更新约束

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

//清楚之前的所有约束,只会保留最新的约束

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

3.常见约束的各种类型

1.尺寸:width、height、size

2.边界:left、leading、right、trailing、top、bottom

3.中心点:center、centerX、centerY

4.边界:edges

5.偏移量:offset、insets、sizeOffset、centerOffset

6.priority()约束优先级(0~1000),multipler乘因数, dividedBy除因数

4.Masonry约束易忽略的技术点

使用Masonry不需要设置控件的translatesAutoresizingMaskIntoConstraints属性为NO;

防止block中的循环引用,使用弱引用(这是错误观点),在这里block是局部的引用,block内部引用self不会造成循环引用的

__weak typeof (self) weakSelf = self;(没必要的写法)

5.Masonry约束控件出现冲突的问题

当约束冲突发生的时候,我们可以设置view的key来定位是哪个view

redView.mas_key = @"redView";

greenView.mas_key = @"greenView";

blueView.mas_key = @"blueView";

若是觉得这样一个个设置比较繁琐,怎么办呢,Masonry则提供了批量设置的宏MASAttachKeys

MASAttachKeys(redView,greenView,blueView); //一句代码即可全部设置

6.equalTomas_equalTo的区别

mas_equalTo只是对其参数进行了一个BOX(装箱) 操作,目前支持的类型:数值类型(NSNumber)、 点(CGPoint)、大小(CGSize)、边距(UIEdgeInsets),而equalTo:这个方法不会对参数进行包装。

7.Masonry布局

make.top.equalTo(view).with.offset(10);// 距离上10

make.left.equalTo(view).with.offset(10);//距离左10

make.bottom.equalTo(view).with.offset(-10);//距离下10

make.right.equalTo(view).with.offset(-10);//距离右10

等同于make.edges.mas_offset(UIEdgeInsetsMake(10,10,10,10));

等高 \等宽

make.height.mas_equalTo(@[redView, blueView]);

make.width.mas_equalTo(@[redView, blueView]);

最大值

make.width.height.lessThanOrEqualTo(@250);

最大放大到整个view make.width.height.lessThanOrEqualTo(self.view);

最小值make.width.height.greaterThanOrEqualTo(@90);

优先级最低

make.width.height.mas_equalTo(100 * self.scacle).priorityLow();

设置高/宽为3:1,要求是同一个控件的属性比例

make.height.mas_equalTo(bottomInnerView.mas_width).multipliedBy(3);

*  axisType         轴线方向

*  fixedSpacing     间隔大小

*  fixedItemLength  每个控件的固定长度/宽度

*  leadSpacing      头部间隔

*  tailSpacing      尾部间隔

//首先添加5个视图

NSMutableArray *array = [NSMutableArray new];

for (int i = 0; i < 5; i ++) {

UIView *view = [UIView new];

view.backgroundColor = [UIColor greenColor];

[self addSubview:view];

[array addObject:view]; //保存添加的控件

}

水平方向控件间隔固定等间隔

[array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:10 tailSpacing:10];

[array makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(50);

make.height.equalTo(70);

}];

水平方向宽度固定等间隔

[array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:70 leadSpacing:10 tailSpacing:10];

[array makeConstraints:^(MASConstraintMaker *make) { //数组额你不必须都是view

make.top.equalTo(50);

make.height.equalTo(70);

}];

设置preferredMaxLayoutWidth:多行label约束的完美解决

[self.label makeConstraints:^(MASConstraintMaker *make) {

make.left.top.equalTo(10);

make.right.equalTo(-10);

}];

更新约束 mas_updateConstraints

// 告诉self.view约束需要更新

[self.view setNeedsUpdateConstraints];

// 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用

[self.view updateConstraintsIfNeeded];

[UIView animateWithDuration:0.3 animations:^{

[self.view layoutIfNeeded];

}];

- (void)updateViewConstraints {

[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) { }];

[super updateViewConstraints];

}

相关文章

  • Masonry 控件详解

    Masonry 控件详解 1.Masonry的属性 @property (nonatomic,strong,rea...

  • 适配

    Masonry iOS自动布局框架-Masonry详解

  • iOS Masonry 等间隔或等宽高排列多个控件

    iOS Masonry 等间隔或等宽高排列多个控件 iOS Masonry 等间隔或等宽高排列多个控件

  • Masonry使用总结

    以下为笔者工作总结,写法看控件设计场景,自行取舍 Masonry详解1:点击跳转 铺满上下左右 上下左右间距为10...

  • Masonry使用方法

    Masonry的使用 Masonry的github地址 Masonry的基本使用方法 给控件添加约束使用Mason...

  • Third Party

    A:推荐使用 B:修改使用 C:参考使用 自动布局Masonry(A)iOS自动布局框架-Masonry详解SDA...

  • 第十八章、拓展一面试问题积累

    一、Masonry设置等高、等宽 子视图等高/等宽练习 等高 等宽 更多详情iOS Masonry详解 二、iOS...

  • iOS资源汇总

    iOS·Objective-C UI控件详解整理 iOS UI控件详解—「UIScrollView滚动视图」 iO...

  • Masnory 使用笔记

    使用Masonry不需要设置控件的translatesAutoresizingMaskIntoConstraint...

  • Masonry源码分析

    Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使...

网友评论

  • 舒马赫:Masory写的很棒,但是不喜欢纯代码写界面,太慢了,另外由于autolayout先天原因布局速度是比较慢的,会影响帧率。推荐使用xml的布局库FlexLib,采用前端布局标准flexbox(不使用autolayout),支持热刷新,自动计算高度等。可以到这里了解详细信息:

    https://github.com/zhenglibao/FlexLib

本文标题:Masonry 控件详解

本文链接:https://www.haomeiwen.com/subject/bhogettx.html