如何以编程方式调整相等高度的AutoLayout乘数?

时间:2016-02-11 09:08:13

标签: ios swift autolayout

基本上我想使用autolayout等高度乘数展开和折叠一堆视图。

我是iOS-Swift Dev的新手

2 个答案:

答案 0 :(得分:0)

数据:

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:

enter image description here

答案 1 :(得分:0)

您可以像这样设置相同的高度约束......

[parentView addConstraint:[NSLayoutConstraint
                          constraintWithItem:childView
                          attribute:NSLayoutAttributeHeight
                          relatedBy:NSLayoutRelationEqual
                          toItem:parentView
                          attribute:NSLayoutAttributeHeight
                          multiplier:1.0
                          constant:0]];

如果需要,您还可以通过划分每个视图的高度来使用乘数。试一次。