一種比較巧妙的設(shè)置約束的方式。
場景:
有兩個 UIView。他們并不一定是父子關(guān)系。
一個 UIView 只設(shè)置x,y 不設(shè)置大小。所以對于它的 frame 而言缺少 width , height.
另外一個可以被內(nèi)容撐大的 UIView,比如 UILabel,UIImageView(包含固有大小的視圖),它設(shè)置成到第一個 UIView 的四邊邊距。
那么這樣的做法之后,由于內(nèi)部的 UIView 的 Width 和 Height 可以被內(nèi)容撐大,所以 對于內(nèi)部 UIView 來說,W,H 有了。
它設(shè)置四邊距的 top , left ,所以它的 x,y 有了。
對于外部視圖,x,y 有了,又由于內(nèi)部視圖設(shè)置了 bottom ,right 的邊距,所以它的 w,h 也有了。
最終效果是:
- 第一個視圖視圖的 x,y w,h 都有了。
- 第二個視圖的 x,y,w,h 也都有了。
演示代碼
UIView *iv = [[UIView alloc] init];
iv.backgroundColor = [UIColor orangeColor];
[self.view addSubview:iv];
CGFloat maxWidth = 200;
[iv mas_makeConstraints:^(MASConstraintMaker *make) {
// 對于 UIView 來說,只設(shè)置了 x,y。
make.left.top.offset(150);
// 設(shè)置一個最大的寬度,寬度抗拉伸。
make.width.lessThanOrEqualTo(@(maxWidth));
}];
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0;
label.text = @"我是 label,我放在了一個只有x,y 的 UIImageView 上面,我里用 edge 可以決定我自己的位置,以及決定 UIImageView 的大小。我是 label,我放在了一個只有x,y 的 UIImageView 上面,我里用 edge 可以決定我自己的位置,以及決定 UIImageView 的大小。我是 label,我放在了一個只有x,y 的 UIImageView 上面,我里用 edge 可以決定我自己的位置,以及決定 UIImageView 的大小。我是 label,我放在了一個只有x,y 的 UIImageView 上面,我里用 edge 可以決定我自己的位置,以及決定 UIImageView 的大小。";
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
// 對于添加到subView 身上的 label 來說,沒有設(shè)置任何的x,y,w,h.但可以通過和某個view 的四邊距的方式來計算自身的 x,y,w,h
// 讓 label 決定 UIImageView 的大小。
// 讓 UIImageView 來決定 Label 的位置
// make.edges.equalTo(imageView).mas_offset(UIEdgeInsetsMake(8, 8, 8, 8));
make.top.left.equalTo(iv).offset(8);
make.right.bottom.equalTo(iv).offset(-8);
}];
運行效果

15133220865886.jpg
開發(fā)中常用的場景
一般是聊天 UI 的氣泡的 UILabel 之間的關(guān)系。
做法:
- 首先設(shè)置外部聊天氣泡的 x,y.
- 在設(shè)置內(nèi)部 UILabel 的相對于外部聊天氣泡的四邊距。
- 【它兩并不是父子關(guān)系?!?/strong>
- 氣泡的x,y 決定了,但沒有寬高。
- UILabel 的 x,y 由本身設(shè)置的 top,left 決定。寬高由 Label 內(nèi)的文本長度決定。
- UILabel 的寬高有了,它的 bottom ,right 就間接的把氣泡的 width,height 確定了。
氣泡只設(shè)置x,y 和一個抗拉伸的寬度
// 聊天背景
UIImageView *rightBubbleView = [[UIImageView alloc] init];
// 普通
rightBubbleView.image = [UIImage imageNamed:@"Dialog_green.right"];
// 高亮
rightBubbleView.highlightedImage = [UIImage imageNamed:@"Dialog_green_press.right"];
[self.contentView addSubview:rightBubbleView];
// 氣泡只設(shè)置x,y 和一個抗拉伸的寬度
[rightBubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(_headerImageView).offset(0);
make.right.equalTo(_headerImageView.mas_left).offset(-10);
}];
UILabel 并不手動設(shè)置 x,y,w,h ,而是使用 edges 來設(shè)置間接的設(shè)置它的x,y,w,h 以及氣泡的 w,h
// 聊天內(nèi)容
_contentLabel = [UILabel labelWithTitle:@"右邊聊天內(nèi)容右邊聊天內(nèi)容右邊聊天內(nèi)容右邊聊天內(nèi)容右邊聊天內(nèi)容右邊聊天內(nèi)容右邊聊天內(nèi)容右邊聊天內(nèi)容右邊聊天內(nèi)容右邊聊天內(nèi)容" titleColor:[UIColor darkGrayColor] fontSize:12];
_contentLabel.numberOfLines = 0;
[self.contentView addSubview:_contentLabel];
CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - (45 + 20) * 2 - 24 - 8;
[_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.lessThanOrEqualTo(@(maxWidth));
make.edges.equalTo(rightBubbleView).mas_offset(UIEdgeInsetsMake(8, 8, 8, 16));
}];
最終效果

15133225347298.jpg