Masonry

1、基本的属性设置
(1)Top, left, bottom, right, width, height
make.top.greaterThanOrEqualTo(superview.top).offset(padding);
make.left.equalTo(superview.left).offset(padding);
make.bottom.equalTo(blueView.top).offset(-padding);
make.right.equalTo(redView.left).offset(-padding);
make.width.equalTo(redView.width);
make.height.equalTo(redView.height);
make.height.equalTo(blueView.height);
(2)center, size
make.center.equalTo(CGPointMake(0, 50));
make.size.equalTo(CGSizeMake(200, 100));
(3)edges
make.edges.equalTo(lastView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
(4)multipliedBy,priorityLow
make.width.equalTo(self.topInnerView.mas_height).multipliedBy(3);
make.width.and.height.lessThanOrEqualTo(self.topView);
make.width.and.height.equalTo(self.topView).with.priorityLow();
make.center.equalTo(self.topView);
2、多行label
- (void)layoutSubviews {
[super layoutSubviews];

  // for multiline UILabel's you need set the preferredMaxLayoutWidth
  CGFloat width = CGRectGetMinX(self.shortLabel.frame) - kPadding.left;
  width -= CGRectGetMinX(self.longLabel.frame);
  self.longLabel.preferredMaxLayoutWidth = width;
  // need to layoutSubviews again as frames need to recalculated with preferredLayoutWidth
  [super layoutSubviews];

}
3、动画效果
// this is Apple’s recommended place for adding/updating constraints
- (void)updateConstraints {

    [self.movingButton remakeConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(@(100));
    make.height.equalTo(@(100));
    if (self.topLeft) {
        make.left.equalTo(self.left).with.offset(10);
        make.top.equalTo(self.top).with.offset(10);
    }
    else {
        make.bottom.equalTo(self.bottom).with.offset(-10);
        make.right.equalTo(self.right).with.offset(-10);
    }
    }];

    //according to apple super should be called at end of method
    [super updateConstraints];

}

- (void)toggleButtonPosition {
    self.topLeft = !self.topLeft;

    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];

    [UIView animateWithDuration:0.4 animations:^{
    [self layoutIfNeeded];
    }];
}

4、数组
(1)固定间距
[arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:5 tailSpacing:5];
[arr makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@60);
make.height.equalTo(@60);
}];
(2)固定控件宽度
[arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:30 leadSpacing:200 tailSpacing:30];
[arr makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@60);
make.height.equalTo(@60);
}];
5、常用方法
mas_makeConstraints() 添加约束
mas_remakeConstraints() 移除之前的约束,重新添加新的约束
mas_updateConstraints() 更新约束,写哪条更新哪条,其他约束不变,即只更新已添加的约束


注意点: 1、当这个View不显示的时候,使用masonry布局没有用


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值