MasonryFloatLayout : 根据Masonry的起浮布局

前语


iOS中运用Masonry进行布局算是日常操作,可是类似于网页的起浮布局的时分,详细示意图如下.

移除其间的某个元素,剩余的元素就会往某个方向进行移动,在Web端,这种布局办法就叫做起浮布局.

MasonryFloatLayout : 根据Masonry的起浮布局

别的还有下面的这种状况,尽管也进行起浮但ios14.4.1更新了什么是仍然还另优先级排序外一边保持着绑缚联络,这一ios下载种在iOS也是比较常见的绑缚状况.

MasonryFloatLayout : 根据Masonry的起浮布局

在iOS的Masonry运用结束上述进程其实非常费事的,假定是咱们根据Masonry,根据这优先级行列种状况,咱们一般会有两种写法,一种是状况穷举法,别的一种数组c言语是暂时视图变量记载法.

状况优先级和劣后级的差异穷举法例:

状况穷举法便是一切的视图组合状况ios8备忘录列举出来然后增加对应的绑缚布局.详细示例能够看下面代码.

- (void)loseConstraintsAction {
if (!_A.hidden && !_B.hidden && !_C.hidden) {
[_A mas_re优先级行列makeConstraints:^(MASConstr优先级最低的运算符是什么aintMaker *ios14make) {
make.优先级是什么意思lefios下载t.equalTo(self.view数组去重的5种办法).offset(20.0f);
}];
[_数组指针B mas_remakeConstrainios是什么意思ts:^(MASConstraintMaker *make) {
make.l数组和链表的差异eft.equ数组c言语alTo(self.A.mas_right).offset(8.0f);
}];
[_C mas_remakeConstrai数组去重nts:^(MASConstraintMaker *make) {
make.left.equalTo(self.B.mas_right).offset(8.0f);
}]ios14;
}
if (!_A.hidden && _B.hidden && !ios14_C.hidden) {
[_Aios下载 mas_remakeCons优先级超越50预定失利traints:^(MASConstraintMaker *make)优先级越小越优先吗 {
make.left.equalTo(self.view).offset(20.0f);
}];
[优先级最低的运算符是什么_C mas_remakeConstraints:^(MASConstraintMaker *make) {数组初始化
make.ios14left.equalTo(self.A.mas_right)数组公式.offset(8.0f);
}];
}
if (_A.hidden &am数组c言语p;&优先级排序 _B.hiddiOSen && !_C.hidden) {
[_C mas_remakeConstraints:^(MASC优先级超越50预定失利onstraintMaker *make) {
makeios14.left.equalTo(self.优先级行列view).offset(8.0f);
}];
}
}

状况穷举法算是非常暴力的计划了.状况穷举法的写法方式多种多样,可是全体思想是一起的, 穷举法尽管了解起来简略, 可是坏处也是清楚明晰的,代码量却是不容小觑,每增加一数组的界说种组合,咱们就需求多一种绑缚计划…..

暂时视图变量记载法:

暂时变量记载法便是运用一个暂时变量记载与哪个视图建立绑缚,详细的代码示例如下所示.

- (void)loseConstraintsAction数组排序 {
UIView *laiOSs数组词tView = self.view;数组
if优先级超越50预定失利 (!_A.hidden) {数组去重的5种办法
[_A mas_remak数组eConst优先级和劣后级的差异raints:^(数组公式MASConstraintMaker *make) {优先级英文
make.left.equalTo([lastView isEqual:self.view] ? lastView : lastView.mas_right).offset(20.0f);
}];
lastView = _A;
}
if (!数组去重_B.hidden) {
[_B mas_remakeConstraints:^(MASCios下载onstraintMaker *make) {
m数组c言语akeiOS.left.equalTo([lastView isEqual:self.vi优先级最低的运算符是什么ew] ? l数组的界说astView : lastView.mas_rightios是什么意思).offset(8.0f);
}];
lastView = _B;
}
if (优先级超越50预定失利!_C.数组去重hidden) {
[_C mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo([lastView isEq优先级是什么意思ual:self.view数组的界说] ? lastView : lastView.mas_right).offset(8.0f);
}];
}
}

比较于穷举法,在代码结构上已经有很大的改观,可是全体代码仍然让人感数组c言语觉不是那么满意.

由此骚栋就在想是否能够根据Masonry封装一个库,来优先级和劣后级的差异结束这种起浮作用呢? 所以我ios14.4.1更新了什么就封装了一个根据Masonry的起浮布局 MasonryFloatLayout.

在说结束之前,咱们先看一下怎么运用封装好的 MasonryFloatLios模拟器ayout.

MasonryFloatLayout的运用


  • 首要先导入Demo中 MasonryFloatLayout 文件夹,如下图所示.

MasonryFloatLayout : 根据Masonry的起浮布局

  • 这儿需数组排序求改动的便是 UIViewFloatLayoutHeader 的Masonry的文件途径地址,假定报错,请导入优先级最低的运算符是什么正确的文件途径地址.
#ifndef UIViewFloatLayoutHeader_h
#define UIViewFloatLayoutHeader_h
#import "Masonry/Masonry.h"
#endif /* UIViewFloatLayoutHe数组排序ader_h */
  • 在需求的ViewController或许View中引进头文件.
#import "Nios14.4.1更新了什么SArray+FloatLayout.h"
  • 假定咱们现在有三个View,需求进行ios模拟器起浮布局,那么咱们先要把初始化作业结束.包含声明View特征和懒加载View办法结束,当然了,你能够按照你的习气来.
@pios8备忘录roperty (nonatomic, strong) UIView *fir优先级表stVie数组公式w;
@property (nonatomic, stron数组排序g) UIView *secondView;
@proios卖肉直播不收费下载perty (nonatomic, st优先级超越50预定失利rong) UILabel *thirdView;
- (UIView *)firstView {
if (_firstView == nil) {
_firstV优先级越小越优先吗iew = [[UIV数组指针iew alloc] initWithFrame:CGRectZero];
_firstView.backgroundColor = [UIColor redColor];
}
return _firstView;
}
- (UIView *)secondView {
if (_secondView == nil) {
_secondV数组指针iew = [[UIView alloc] initWithFrame:CG优先级行列RectZero];
_secondView.backgroundColor = [UIColor orangeColor];
}
return _secondView;
}
- (UILabel *)thirdView {
if (_thirdView == nil) {
_thirdView = [[UILabel a数组公式lloc] initWithFrame:CGRectZ数组指针ero];
_数组和链表的差异thirdView.数组的界说backiOSgroundColor = [UIColor blueColor];
_thirdView.textC数组指针olor = [UIColor whiteColor];
_thi优先级英文rdView.text = @"333333333333";
}
return _thirdView;
}
  • 接下来咱们需求起浮绑缚布局的增加了.和Masonios卖肉直播不收费下载ry布局相同,首要咱们需求数组排序运用mas优先级最高的运算符_remakeFloatLayoutConstraints给每一个View增加详细优先级表的布局.毕竟再用数组增加起浮布局.详细代码如下所示.
    [self.firstView mas_remakeFloatLayoutConstraints:^(MASConstraintMa优先级行列ker * _Nonnull make, UIView * _Nonnull lastView, UIVios14iew * _Nonnull nextView) {
make.left.equalTo(@50);
make.height.equalTo(@100);
make.width.equalTo(@100);
make.lastFloatConstraint.offset(10.0f);
make.nextFloatConstra数组排序intios卖肉直播不收费下载.offset(-1优先级越小越优先吗0.0f).priorityHigh();
}];
[self.secondView mas_remake数组去重的5种办法Flo优先级表atLayoutConstraints:^(MASios8备忘录ConstraintMaker * _Nonnull make, UIView * _Nonnull lastView, UIView * _Nonnull nextView) {
make.left.equalTo(@50);
make.height.equalTo(@1iOS00);
make.width.equalTo(@100);
make.las优先级最高的运算符tFloatConstraint.offset(30.0f).priorityLow();
make.nextFloatConstraint.offset(-10.0f);
}];
[self.thirdView mas_remakeFloatLayoutConstra优先级超越50预定失利ints:^(MASConstraintMaker * _Nonnull make, UIView * _Non数组词null lastView, UIView * _Nonnull nextView) {
make.left.equalTo(@50);
makeios卖肉直播不收费下载.height.equalTo(@100)ios退款;
make.lastFloatConstraint.offset(10.0f);
make.数组去重nextFloa数组去重的5种办法tConstraint.offset(-10.0f);数组和链表的差异
}];
[@[self.thirdView, self.firstView, self.secondView] mas_remakeFloatLayoutConst优先级行列raintsWithOrientation:FloatLayoutOrientati优先级排序onBottomToTop needLastConstraint:need];
  • 在上面增加绑缚进程中,咱们需求运用 lastFloatConstraintnextFloatConstraintios14,这两个特征都是来源于 MASConstraintMaker+Floaios卖肉直播不收费下载tLayout,咱们能够经过这两个特征增加与上一个View和下一个View的距离联络.
@property (nonatomios8备忘录ic, strong) MASFloatLayoutConstraint *lastFloatConstraint;
@property (nonatomic, strong) MASFloatLayoutConstraint *nextFloatConstraint;
  • 当然了,距离联络也能够设置优先级,这样的话.假定有距离绑缚优先级是什么意思抵触,经过设置优先级来处理这种问题.这在上面的示例中也是有所表现.
- (MASFloatLayoutConstraint * (^)(MASLayoutPriority priority))priorityios系统;
- (MASFloatLayoutConstraint * (^)(void))priorityLow;
- (MASFloatLayoutConstraint * (^)(void))priorityMedium;
- (MASFloatLayoutConstraint * (^)(voiios是什么意思d))priorityHigh;
  • maios模拟器s_remakeFloatLayoutConstraints 办法的Block参数除了常见的MASConstraiios下载ntMaker *make 之外,还会回来上一个视图lastView和下一个视图nextView,当然了,这两个参数有可能是nil.
typedef void(^优先级最低的运算符是什么FloatConstr数组的界说aintMaker)(MASConstraintMaker *make, UIView *lastView, UIView *nextVi优先级行列ew);
  • 每一个视图的绑缚增加结束之后,咱们增加起浮布局,起浮布局是根据NSArray的分类 NSArray+FloatLayout, mas_remakeFios模拟器loatLayoutConstraintsWithOrientation 办法总共有两个参数.一个是用来设定起浮的方向,别的一个是用来设定毕竟一个视图与父视图的联络,类似于上一个模块提到优先级行列的第二种状况.
typedef enum : NSUInte优先级和劣后级的差异ger {
FloatLayoutOrien数组词tationUnknow,      // 不知道或许根据自界说的绑缚进行绑缚布局
FloatLayoutOrientationLeftToRight,优先级最低的运算符是什么 // 从左到右进行布局
FloatLayoutOrientatios8备忘录ionRightToLeft, // 从右优先级到左进行布局
FloatLayoutOrientationTopToBottom, // 从上到下进行布局
FloatLayoutOrientationBottomTios8备忘录oTop, // 从下到上进行布局
} FloatLayoutOrientation;
/// 履行起浮布局
/// @param orientation 相对于父视图的起浮方向
/// @param needLastConstraint 是否需求增加毕竟的绑缚优先级表
- (void)mas_remakeFloatLayoutCon优先级最高的运算符straintsWithOrientation:(FloatLayoutOrientation)orientation
needLastConstr数组和链表的差异aint:(BOOL)needLastConstraint;

全体的运用流程便是这样的.全体来说和本来的Masonry布局代码办法没有什么太大差异.详细的示例能够看一下我的Demo.

总结


假定在运用进程中遇到任何问题欢迎随时找我,骚栋在这儿谢谢大家了数组公式.

Masonry优先级和劣后级的差异FloatLayout传送门

MasonryFloatLayout : 根据Masonry的起浮布局