【轉(zhuǎn)】iOS:Masonry小記

轉(zhuǎn)自:https://www.cnblogs.com/XYQ-208910/p/5011483.html

Masonry練習(xí)詳解

添加約束的方式:

1.通過使用NSLayoutConstraints添加約束到約束數(shù)組中,之前必須設(shè)置translatesAutoresizingMaskIntoConstraints = NO,即取消自動布局;

2.通過使用MASConstraintMaker在block中添加約束,不需要再設(shè)置translatesAutoresizingMaskIntoConstraintst 屬性,block內(nèi)部已經(jīng)幫助完成;

約束的關(guān)系:

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

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

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

MASViewAttribute:視圖約束屬性

image

UIView/NSView

這兩個約束完全相同,都是view左邊大于等于label的左邊位置

make.left.greaterThanOrEqualTo(label);

make.left.greaterThanOrEqualTo(label.mas_left);

NSNumber給約束設(shè)置具體的值

<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,使用原始的數(shù)據(jù)或者結(jié)構(gòu)體設(shè)置約束數(shù)據(jù)</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;">使用數(shù)組NSArray設(shè)置約束</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>

使用優(yōu)先級設(shè)置約束

.priorityHigh <======> UILayoutPriorityDefaultHigh 高優(yōu)先級

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

.priorityLow <=========> UILayoutPriorityDefaultLow 低優(yōu)先級

<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>

有時候,你需要修改現(xiàn)有的約束,以動畫或刪除/替換約束。在砌體中有幾個不同的方法來更新約束。

1.使用設(shè)置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.重新設(shè)置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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容