Masonry 笔记

作者: 天空中的球 | 来源:发表于2015-12-14 17:30 被阅读1191次

最近每周末都会看叶孤城的直播,对于我们iOS开发者来说,确实是一个福利,很感谢他们的分享精神,收获到的一些东西特此记录下。

12月12号 Masonry

昨天听了里脊串的视频之后,对Masonry 有进一步了解。

Masonry的进一步认识

以前用Masonry感觉就是把一些约束条件一加,就完事了,没有进行一些细致的了解。

首先看一下系统的,通过这七个参数来大致了解下

[NSLayoutConstraint constraintWithItem:self.testRedView   // 第一个视图
                             attribute:NSLayoutAttributeTop // 第一个视图的属性attr1
                             relatedBy:NSLayoutRelationEqual // 两个视图的关系
                                toItem:self.view // 第二个视图
                             attribute:NSLayoutAttributeTop // 第二个视图的属性attr1
                            multiplier:1.0 //乘
                              constant:10]; //常量

 相当于//    view1.attr1 <relation> view2.attr2 * multiplier + constant

而Masonry中可以分为这五个参数来了解

  Attribute(属性) ,Relation(关系),Multiplier(乘),Constant(大小),Priority(优先级)
Attribute
串哥的图片1

leadingtrailing 是和 leftright是相同的,只是考虑到阿拉伯国家不用左右而定的,官方就用前面一组的。

Relation
typedef NS_ENUM(NSInteger, NSLayoutRelation) {
    NSLayoutRelationLessThanOrEqual = -1, // 小于或等于
    NSLayoutRelationEqual = 0, // 相等
    NSLayoutRelationGreaterThanOrEqual = 1, //大于或等于
};
Multiplier

简单的说,就是两种之间的倍数,例如经常使用

make.width.equalTo(self.view.mas_width).multipliedBy(0.5);           
Constant

就是我们常用的常量

串哥的图片2
Priority 优先级
串哥的图片3
static const UILayoutPriority UILayoutPriorityRequired NS_AVAILABLE_IOS(6_0) = 1000; 
static const UILayoutPriority UILayoutPriorityDefaultHigh NS_AVAILABLE_IOS(6_0) = 750; 
static const UILayoutPriority UILayoutPriorityDefaultLow NS_AVAILABLE_IOS(6_0) = 250; 
static const UILayoutPriority UILayoutPriorityFittingSizeLevel NS_AVAILABLE_IOS(6_0) = 50;
Masonry注意点一

有时我们用的是NavgationController,会导致我们如果直接用

make.top.equalTo(self.view.as_top);

会被状态栏遮住.

遮住状态栏

所以此时我们需要注意下面几个属性啦

串哥的图片4

此时可以用

 make.top.equalTo(self.mas_topLayoutGuideBottom);

代替上面 make.top.equalTo(self.view.as_top)

或者采取下面的方法,都能解决遮住状态栏的问题

self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;
Masonry注意点二
串哥的图片5

intrinsicContentSize

这个方法在我们没有指定高度或宽度的时候,它会自动帮我们确定宽度或高度,特别是用于UILabel的时候是相当方便的。

- (UILabel *)testLabel
{
    if(!_testLabel)
    {
        _testLabel = [[UILabel alloc] init];
        _testLabel.text = @"hjsfjhfkjfskfsdkjfdskj";
        _testLabel.numberOfLines = 0;
        _testLabel.backgroundColor = [UIColor redColor];
    }
    return _testLabel;
}

[self.view addSubview:self.testLabel];
[self.testLabel mas_makeConstraints:^(MASConstraintMaker * make){

   
    make.width.equalTo(@100);
    make.top.equalTo(@100);
    make.left.equalTo(@50);

}];
自动计算高度
Content Priority 拉伸和压缩
串哥的图片-拉伸和压缩
 [self.testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
 [self.testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];

这个要根据情况而定,有时会很有用的。

  • setContentHuggingPriority: 优先级越高,代表压缩越厉害,越晚被拉伸。就是上图中那人拉的力量更强。
  • setContentCompressionResistancePriority:: 优先级越高,代表拉伸越厉害,越晚被压缩。就是上图那人推的力量更强。
Masonry注意问题三
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

/*
  mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错 
  mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
  mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束

  三种函数善加利用 就可以应对各种情况了
*/

设置动画改变的时候,就是改变某一项属性,位置或大小,此时需要用到mas_updateConstraints:

Masonry中ScrollerView

注意的是UIScrollView依靠与其subviews之间的约束来确定ContentSize的大小,所以它很特殊,我们添加约束的时候,常常额外给其添加一个ContentView,控制subViews。

UIView *container = [[UIView alloc] init];
[testScrollView addSubview:container];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(testScrollView);
    make.width.equalTo(testScrollView);
}];

然后最后的时候最后再根据subViews更新达到效果。

[container mas_updateConstraints:^(MASConstraintMaker *make) {
    make.bottom.mas_equalTo(subView.mas_bottom).with.offset(40);
}];

详细的可以看串哥这篇博客.

备注

很感谢串哥,这是里脊串的博客,特此分享。作为同是长沙人的我,真是我学习的榜样啊,⛽️。

相关文章

  • masonry

    为什么要做这个笔记 masonry [https://github.com/SnapKit/Masonry]可以说...

  • iOS框架·Masonry源码深度解析及学习启示:设计模式与链式

    传送门:链式编程小Demo 这篇文章是 Masonry 框架源码的解析和笔记。学习Masonry之前,先了解这个框...

  • Masonry笔记

    - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMa...

  • Masonry 笔记

    最近每周末都会看叶孤城的直播,对于我们iOS开发者来说,确实是一个福利,很感谢他们的分享精神,收获到的一些东西特此...

  • Masonry 笔记

    类似top和mas_top的区别 top是MASConstraintMaker的属性,mas_top是view的分...

  • Masonry 介绍 2018-01-29

    介绍 Masonry 源码:https://github.com/Masonry/Masonry Masonry是...

  • masonry 使用笔记--续

    序 上一篇做完了masonry 的基础用法笔记,现在想要更深入一些了解 masonry 对动画框架的支持,再进一步...

  • 关于Masonry小记

    Masonry 源码:https://github.com/Masonry/Masonry Masonry是一个轻...

  • Masonry的用法

    Masonry 源码:https://github.com/Masonry/Masonry; 看一下Masonry...

  • Masonry学习笔记

    Masonry Masonry是一个轻量级的布局框架,采用链式语法封装自动布局,简明,高可读。 通常leading...

网友评论

    本文标题:Masonry 笔记

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