[英]Programmatically setting layout constraints in a UICollectionViewCell
[英]Setting layout constraints on a set of views programmatically
我有三個UIImageView
(彼此水平放置),我想在self.view
內水平和垂直居中,它們之間具有固定的水平間距。 我不太了解如何使用布局約束,並且知道將它們放置在中間容器中,然后適當地設置autoresizingMask
可以達到目的:
containerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
在不使用中間容器的情況下要達到相同的效果會怎樣?
假設您的視圖中有圖像視圖leftImageView
, rightImageView
和centerImageView
,這些約束應該可以解決問題:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerImageView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerImageView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[leftImage]-[centerImage]-[rightImage]"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:@{@"leftImage" : leftImageView,
@"rightImage" : rightImageView,
@"centerImage" : centerImageView}]];
確保每個圖像視圖上的translatesAutoresizingMaskIntoConstraints
為false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.