美文网首页
Masonry的用法

Masonry的用法

作者: 玉米地里种玉米 | 来源:发表于2016-08-26 15:16 被阅读475次

    Masonry 源码:https://github.com/Masonry/Masonry

    看一下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;

    由于block的内存原因  :进行实例之前先介绍一个宏:(MACRO)

    #define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

    1、居中显示一个View

    WS(ws);

    UIView *sv = [UIViewnew];

    注意:使用之前需要先将视图添加到父视图

    [sv mas_makeConstraints:^(MASConstraintMaker *make) {

    make.center.equalTo(ws.view);

    make.size.mas_equalTo(CGSizeMake(300, 300));

    }];

    首先在Masonry中能够添加autolayout约束有三个函数:

    - (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 则会清除之前的所有约束 仅保留最新的约束

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

    */

    2. [初级] 让一个view略小于其superView(边距为10)

    UIView *sv1 = [UIViewnew];

    sv1.backgroundColor = [UIColor redColor];

    [sv addSubview:sv1];

    [sv1 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

    /* 等价于

    make.top.equalTo(sv).with.offset(10);

    make.left.equalTo(sv).with.offset(10);

    make.bottom.equalTo(sv).with.offset(-10);

    make.right.equalTo(sv).with.offset(-10);

    */

    /* 也等价于

    make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

    */

    }];

    可以看到 edges 其实就是top,left,bottom,right的一个简化 分开写也可以 一句话更省事

    那么为什么bottom和right里的offset是负数呢? 因为这里计算的是绝对的数值 计算的bottom需要小鱼sv的底部高度 所以要-10 同理用于right

    这里有意思的地方是and和with 其实这两个函数什么事情都没做

    - (MASConstraint *)with{

    returnself;

    }

    - (MASConstraint *)and {

    returnself;

    }

    [初级] 让两个高度为150的view垂直居中且等宽且等间隔排列 间隔为10(自动计算其宽度)

    int padding1 = 10;

    [sv2 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.centerY.mas_equalTo(sv.mas_centerY);

    make.left.equalTo(sv.mas_left).with.offset(padding1);

    make.right.equalTo(sv3.mas_left).with.offset(-padding1);

    make.height.mas_equalTo(@150);

    make.width.equalTo(sv3);

    }];

    [sv3 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.centerY.mas_equalTo(sv.mas_centerY);

    make.left.equalTo(sv2.mas_right).with.offset(padding1);

    make.right.equalTo(sv.mas_right).with.offset(-padding1);

    make.height.mas_equalTo(@150);

    make.width.equalTo(sv2);

    }];

    这里我们在两个子view之间互相设置的约束 可以看到他们的宽度在约束下自动的被计算出来了

    [中级] 在UIScrollView顺序排列一些view并自动计算contentSize:

    UIScrollView *scrollView = [UIScrollViewnew];

    scrollView.backgroundColor = [UIColor whiteColor];

    [sv addSubview:scrollView];

    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {

    make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(5,5,5,5));

    }];

    UIView *container = [UIViewnew];

    [scrollView addSubview:container];

    [container mas_makeConstraints:^(MASConstraintMaker *make) {

    make.edges.equalTo(scrollView);

    make.width.equalTo(scrollView);

    }];

    int count = 10;

    UIView *lastView = nil;

    for( int i = 1 ; i <= count ; ++i )

    {

    UIView *subv = [UIViewnew];

    [container addSubview:subv];

    subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )

    saturation:( arc4random() % 128 / 256.0 ) + 0.5

    brightness:( arc4random() % 128 / 256.0 ) + 0.5

    alpha:1];

    [subv mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.and.right.equalTo(container);

    make.height.mas_equalTo(@(20*i));

    if( lastView )

    {

    make.top.mas_equalTo(lastView.mas_bottom);

    }

    else

    {

    make.top.mas_equalTo(container.mas_top);

    }

    }];

    lastView = subv;

    }

    [container mas_makeConstraints:^(MASConstraintMaker *make) {

    make.bottom.equalTo(lastView.mas_bottom);

    }];

    从scrollView的scrollIndicator可以看出 scrollView的内部已如我们所想排列好了

    这里的关键就在于container这个view起到了一个中间层的作用 能够自动的计算uiscrollView的contentSize

    [高级] 横向或者纵向等间隙的排列一组view:

    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];

    for( int i = 0 ; i < views.count+1 ; ++i )

    {

    UIView *v = [UIViewnew];

    [spaces addObject:v];

    [self addSubview:v];

    [v mas_makeConstraints:^(MASConstraintMaker *make) {

    make.width.equalTo(v.mas_height);

    }];

    }

    UIView *v0 = spaces[0];

    __weak __typeof(&*self)ws = self;

    [v0 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.equalTo(ws.mas_left);

    make.centerY.equalTo(((UIView*)views[0]).mas_centerY);

    }];

    UIView *lastSpace = v0;

    for( int i = 0 ; i < views.count; ++i )

    {

    UIView *obj = views[i];

    UIView *space = spaces[i+1];

    [obj mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.equalTo(lastSpace.mas_right);

    }];

    [space mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.equalTo(obj.mas_right);

    make.centerY.equalTo(obj.mas_centerY);

    make.width.equalTo(v0);

    }];

    lastSpace = space;

    }

    [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {

    make.right.equalTo(ws.mas_right);

    }];

    }

    - (void) distributeSpacingVerticallyWith:(NSArray*)views

    {

    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];

    for( int i = 0 ; i < views.count+1 ; ++i )

    {

    UIView *v = [UIViewnew];

    [spaces addObject:v];

    [self addSubview:v];

    [v mas_makeConstraints:^(MASConstraintMaker *make) {

    make.width.equalTo(v.mas_height);

    }];

    }

    UIView *v0 = spaces[0];

    __weak __typeof(&*self)ws = self;

    [v0 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.top.equalTo(ws.mas_top);

    make.centerX.equalTo(((UIView*)views[0]).mas_centerX);

    }];

    UIView *lastSpace = v0;

    for( int i = 0 ; i < views.count; ++i )

    {

    UIView *obj = views[i];

    UIView *space = spaces[i+1];

    [obj mas_makeConstraints:^(MASConstraintMaker *make) {

    make.top.equalTo(lastSpace.mas_bottom);

    }];

    [space mas_makeConstraints:^(MASConstraintMaker *make) {

    make.top.equalTo(obj.mas_bottom);

    make.centerX.equalTo(obj.mas_centerX);

    make.height.equalTo(v0);

    }];

    lastSpace = space;

    }

    [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {

    make.bottom.equalTo(ws.mas_bottom);

    }];

    next:

    UIView *sv11 = [UIViewnew];

    UIView *sv12 = [UIViewnew];

    UIView *sv13 = [UIViewnew];

    UIView *sv21 = [UIViewnew];

    UIView *sv31 = [UIViewnew];

    sv11.backgroundColor = [UIColor redColor];

    sv12.backgroundColor = [UIColor redColor];

    sv13.backgroundColor = [UIColor redColor];

    sv21.backgroundColor = [UIColor redColor];

    sv31.backgroundColor = [UIColor redColor];

    [sv addSubview:sv11];

    [sv addSubview:sv12];

    [sv addSubview:sv13];

    [sv addSubview:sv21];

    [sv addSubview:sv31];

    //给予不同的大小 测试效果

    [sv11 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.centerY.equalTo(@[sv12,sv13]);

    make.centerX.equalTo(@[sv21,sv31]);

    make.size.mas_equalTo(CGSizeMake(40, 40));

    }];

    [sv12 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.size.mas_equalTo(CGSizeMake(70, 20));

    }];

    [sv13 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.size.mas_equalTo(CGSizeMake(50, 50));

    }];

    [sv21 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.size.mas_equalTo(CGSizeMake(50, 20));

    }];

    [sv31 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.size.mas_equalTo(CGSizeMake(40, 60));

    }];

    [sv distributeSpacingHorizontallyWith:@[sv11,sv12,sv13]];

    [sv distributeSpacingVerticallyWith:@[sv11,sv21,sv31]];

    [sv showPlaceHolderWithAllSubviews];

    [sv hidePlaceHolder];

    相关文章

      网友评论

          本文标题:Masonry的用法

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