美文网首页
【转】iOS:Masonry小记

【转】iOS:Masonry小记

作者: Sonoface | 来源:发表于2021-01-29 13:17 被阅读0次

    转自:https://www.cnblogs.com/XYQ-208910/p/5011483.html

    Masonry练习详解

    添加约束的方式:

    1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConstraints = NO,即取消自动布局;

    2.通过使用MASConstraintMaker在block中添加约束,不需要再设置translatesAutoresizingMaskIntoConstraintst 属性,block内部已经帮助完成;

    约束的关系:

    equalTo <=======> NSLayoutRelationEqual 等于

    lessThanOrEqualTo <======> NSLayoutRelationLessThanOrEqual 小于或等于

    greaterThanOrEqualTo <=======> NSLayoutRelationGreaterThanOrEqual 大于或等于

    MASViewAttribute:视图约束属性

    image

    UIView/NSView

    这两个约束完全相同,都是view左边大于等于label的左边位置

    make.left.greaterThanOrEqualTo(label);

    make.left.greaterThanOrEqualTo(label.mas_left);

    NSNumber给约束设置具体的值

    <1>//width >= 200 && width <= 400

    <pre style="margin: 0px; padding: 0px; overflow: auto;">make.width.greaterThanOrEqualTo(@200);
    make.width.lessThanOrEqualTo(@400)</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;"><2>//creates view.left = view.superview.left + 10</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">make.left.lessThanOrEqualTo(@10)</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">代替NSNumber,使用原始的数据或者结构体设置约束数据</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">make.top.mas_equalTo(42);
    make.height.mas_equalTo(20);
    make.size.mas_equalTo(CGSizeMake(50, 100));
    make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
    make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">使用数组NSArray设置约束</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">make.height.equalTo(@[view1.mas_height, view2.mas_height]);
    make.height.equalTo(@[view1, view2]);
    make.left.equalTo(@[view1, @100, view3.right]);</pre>

    使用优先级设置约束

    .priorityHigh <======> UILayoutPriorityDefaultHigh 高优先级

    .priorityMedium <========> between high and low ** 介于高/低之间**

    .priorityLow <=========> UILayoutPriorityDefaultLow 低优先级

    <pre style="margin: 0px; padding: 0px; overflow: auto;">make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
    make.top.equalTo(label.mas_top).with.priority(600);</pre>

    使用MASCompositeConstraints添加约束

    edges:边缘

    <pre style="margin: 0px; padding: 0px; overflow: auto;">// make top, left, bottom, right equal view2
    make.edges.equalTo(view2);

    // make top = superview.top + 5, left = superview.left + 10,
    // bottom = superview.bottom - 15, right = superview.right - 20
    make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">// All edges but the top should equal those of the superview
    make.left.right.and.bottom.equalTo(superview);
    make.top.equalTo(otherView);</pre>

    size:大小

    <pre style="margin: 0px; padding: 0px; overflow: auto;">// make width and height greater than or equal to titleLabel
    make.size.greaterThanOrEqualTo(titleLabel)

    // make width = superview.width + 100, height = superview.height - 50
    make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))</pre>

    center:中心

    <pre style="margin: 0px; padding: 0px; overflow: auto;">// make centerX and centerY = button1
    make.center.equalTo(button1)

    // make centerX = superview.centerX - 5, centerY = superview.centerY + 10
    make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))</pre>

    有时候,你需要修改现有的约束,以动画或删除/替换约束。在砌体中有几个不同的方法来更新约束。

    1.使用设置References

    <pre style="margin: 0px; padding: 0px; overflow: auto;">// in public/private interface
    @property (nonatomic, strong) MASConstraint *topConstraint;
    ...
    // when making constraints
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
    }];
    ...
    // then later you can call
    [self.topConstraint uninstall];</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">2.更新约束 mas_updateConstraints</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">- (void)updateConstraints {
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self);
    make.width.equalTo(@(self.buttonSize.width)).priorityLow();
    make.height.equalTo(@(self.buttonSize.height)).priorityLow();
    make.width.lessThanOrEqualTo(self);
    make.height.lessThanOrEqualTo(self);
    }];

    //according to apple super should be called at end of method
    

    [super updateConstraints];
    }</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">3.重新设置mas_remakeConstraints</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;">- (void)changeButtonPosition {</pre>

    <pre style="margin: 0px; padding: 0px; overflow: auto;"> [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.size.equalTo(self.buttonSize);

        if (topLeft) {
            make.top.and.left.offset(10);
        } else {
            make.bottom.and.right.offset(-10);
        }
    }];
    

    }</pre>

    相关文章

      网友评论

          本文标题:【转】iOS:Masonry小记

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