Masonry - 自动布局

第三方库地址:https://github.com/SnapKit/Masonry
pod 'Masonry'

(UIKit - 中有系统的自动布局)

一般的布局

    self.letfView = [UIView new];
    self.letfView.backgroundColor = [UIColor redColor];
    self.rightView = [UIView new];
    self.rightView.backgroundColor = [UIColor greenColor];

    [self.view addSubview:self.letfView];
    [self.view addSubview:self.rightView];

    [self.letfView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).mas_offset(10);// leftView 左边 = self.view 左边 +10
        make.top.mas_equalTo(self.view).mas_offset(20);// leftView 上边 = self.view 上边 +20
        make.height.mas_equalTo(100);// leftView 高 = 100
    }];

    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.letfView);// rightView 上边 = leftView 上边(即上边对齐)
        make.right.mas_equalTo(self.view).mas_offset(-10);// rightView 右边 = self.view 右边 - 10
        make.left.mas_equalTo(self.letfView.mas_right).mas_offset(20);// rightView 左边 = leftView 右边 + 20
        make.width.mas_equalTo(self.letfView);// rightView 宽 = leftView 宽
        make.height.mas_equalTo(self.letfView);// rightView 高 = leftView 高
    }];

// 实现了最基础的 左右2个View 等高等宽等简单约束,在横竖屏切换通用。

到这里,基本上已经可以开始玩masonry了,看完更多属性,基本小学毕业了。

更多 make.XXX ,先手比较属性。

left; 左
top; 上
right; 右
bottom; 下
leading; 左
trailing; 右
width; 宽
height; 高
centerX; x轴中心
centerY; y轴中心
baseline; 基线,没怎么用过

leftMargin; 左边默认边距好像是20,下面的类似
rightMargin;
topMargin;
bottomMargin;
leadingMargin;
trailingMargin;
centerXWithinMargins;
centerYWithinMargins;

----------- 分割线 ----------

edges;4周
size;大小
center;中心
对应语法略有不同,偏移方法也相对复杂一些。不偏移的话使用可以与上面一致。

更多mas_equalTo(XXX) ,后手比较属性

上半部分 基本雷同,如果不添加,就是默认与前面make.XXX 对应的mas_
mas_left;
mas_top;
mas_right;
mas_bottom;
mas_leading;
mas_trailing;
mas_width;
mas_height;
mas_centerX;
mas_centerY;
mas_baseline;

mas_leftMargin;
mas_rightMargin;
mas_topMargin;
mas_bottomMargin;
mas_leadingMargin;
mas_trailingMargin;
mas_centerXWithinMargins;
mas_centerYWithinMargins;

----------- 分割线 ----------

自动根据bar 高度设置的引导属性值,举个例子:
存在navigationBar 时,mas_topLayoutGuideBottom 相当于 增加了44。
不存在navigationBar 时,mas_topLayoutGuideBottom 相对于 0 。

mas_topLayoutGuide;// navgationBar 相关,
mas_topLayoutGuideTop;
mas_topLayoutGuideBottom;

mas_bottomLayoutGuide;// tabbar toolbar 相关
mas_bottomLayoutGuideTop;
mas_bottomLayoutGuideBottom;

多条件布局

  • 大于、小于、等于
mas_equalTo; =
mas_greaterThanOrEqualTo; >=
mas_lessThanOrEqualTo; <=
// 大小于,使用场景感觉比较少。

  • 约束优先级
简单举例:原本2个View是上下排布,当topView 移除时,bottomView,就使用次级约束而上移。

    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.right.and.left.mas_equalTo(self.view);
        make.height.mas_equalTo(100);
    }];

    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom).priority(999);// 可以自己写优先值,越大越优先(1000 是xib 默认值,也是系统默认最大值)
        make.top.mas_equalTo(self.view).priorityLow();// 也可以用系统默认的优先值,low 代表250

        make.left.and.right.mas_equalTo(self.view);
        make.height.mas_equalTo(100);
    }];

  • 同时多个属性一致简写
// 使用 and 链接属性
make.left.and.top.and.width.and.height.mas_equalTo(self.letfView);

  • 同时与多个View存在关系,混合写
// 数组形式    make.top.mas_equalTo(@[self.letfView.mas_top,self.rightView.mas_top]);

  • mas_equalTo(XXX) 的 mas_width 与 width 比较
// mas_ 前缀的 是宏定义,封装好了直接可以使用 NSInteger,
// 而没有前缀的 需要使用 NSNumber
        make.width.mas_equalTo(100);
        make.width.equalTo(@100);

  • mas_offset 与 with.offset 相比较

    UIEdgeInsets edg = UIEdgeInsetsMake(10, 20, 30, 40);

    [self.letfView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view).mas_offset(edg);
        make.edges.mas_equalTo(self.view).with.insets(edg);

    }];

// 使用 with. 需要区分 offset、insets、sizeOffset、centerOffset,分别使用偏移。
// 使用mas_offset 自动区分了上面的几种情况,自动匹配了

关于 偏移 可以继续研究CoreGraphice 框架中的 CGGeometry,下次在写。

更新 重置约束 - 可以实现动画效果

设置约束三大方法

mas_makeConstraints 添加某个
mas_updateConstraints 更新某个
mas_remakeConstraints 重置全部

  • 简单更新约束
    // 原本 约束
    [self.letfView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.right.and.left.mas_equalTo(self.view);
        make.height.mas_equalTo(100);
    }];

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 更新约束
    [self.letfView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(300);
    }];

  • 结合平移动画
    // 修改 上面的 更新约束的方法,
    [self.letfView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(300);
    }];

    [self.letfView setNeedsLayout]; // 标记需要更新约束
    [UIView animateWithDuration:1. animations:^{
        [self.letfView layoutIfNeeded]; // 动画过程中更新约束,达到偏移效果
    }];

布局涉及的其他属性 - 内容抗压缩,内容紧凑

(我的理解,叫法可能不一样)

举个例子:同水平方向有2 个 Label,内容都不确定有多少,即宽度不定。

  • 对2个Label 进行不定宽的布局,如果仅仅这样布局,对内容的控制,可能不是我们想要的。但是好像没有报错。可能默认处理了。(我这里尝试与第一个label 抗压缩,第二个label 内容优先紧凑,效果一致。)

    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.and.left.mas_equalTo(self.view);
    }];

    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.and.right.mas_equalTo(self.view);
        make.left.mas_equalTo(self.leftLabel.mas_right);
    }];

  • 对Label 添加属性
(个人觉得第一步是这个:先显示内容)
    // 内容紧凑 - 优先完全显示内容,且不多占像素。
    [self.leftLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.rightLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

// (个人觉得第二步是这个:调整内容是否压缩)
    // 抵抗 压缩,抗压缩低的,在上一步的基础上,进行压缩调整。
    [self.leftLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.rightLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

作者:说了是村长
链接:https://www.jianshu.com/p/47f01594d031
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容