![](/img/trans.png)
[英]How to center two views in super view with greater than or equal to constraints
[英]How to stack + center two text views using programatic constraints?
我正在嘗試實現一種硬編碼布局,在該布局中,兩個文本視圖應相互堆疊,並以父UICollectionViewCell為中心:
----------------------
| |
| This is text |
| Also text |
| |
----------------------
由於各種遺留/業務原因,我應該使用在UICollectionViewCell的子類中硬編碼的約束來執行此操作。 這兩個文本視圖的長度可以不同,但應在父視圖中垂直居中,並且彼此重疊。
有沒有一種簡單的方法可以在約束條件下表達這一點? 我對這種布局系統有點陌生,因此不勝感激!
我正在使用的應用程序也使用Masonry( https://github.com/SnapKit/Masonry )庫,如果這樣會使事情變得更容易的話。
假設標簽分別命名為textView1
和textView2
。
你需要的是設定一個約束的水平居中textView1
它是superview
(該UICollectionViewCell
),然后中心textView2
與textView1
(你可以向中心是superview
太),你將有兩個中心。
為了使它彼此textView2
,必須設置一個約束,將textView2
top設置為textView1
bottom。
從未使用過砌築,但是看起來您需要具有以下約束:
[textView1 mas_makeConstraints:^(MASConstraintMaker *make) {
//Center first textView in the superview
make.centerX.equalTo(superview);
}];
[textView2 mas_makeConstraints:^(MASConstraintMaker *make) {
//Center second textView with the first one
make.centerX.equalTo(textView1);
//Set second textView to be below the first one
make.top.equalTo(textView1.mas_bottom);
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.