![](/img/trans.png)
[英]How to set autolayout constraint so that the height of an UIImageView is equal to the height of the superview?
[英]How to adjust equal height AutoLayout multiplier programatically?
基本上,我想使用自動布局等高倍增器來展開和折疊一堆視圖。
我是iOS-Swift開發人員的新手
數據:
let objectA = UIView()
let objectB = UIView()
let views = ["objectA": objectA, "objectB": objectB] // (needed for the second option)
views.values.forEach {
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
選項1:
view.addConstraint(NSLayoutConstraint(item: objectA, attribute: .Height, relatedBy: .Equal, toItem: objectB, attribute: .Height, multiplier: 1, constant: 0))
選項2:
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[objectA(100)]", options: [], metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[objectB(==objectA)]", options: [], metrics: nil, views: views))
選項3:
objectA.heightAnchor.constraintEqualToAnchor(objectB.heightAnchor).active = true
選項4:
您可以像這樣設置相等的高度約束...
[parentView addConstraint:[NSLayoutConstraint
constraintWithItem:childView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0]];
如果需要,還可以通過除以每個視圖的高度來使用乘數。 嘗試一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.