如何更改StackView中存在的视图的高度

时间:2019-10-24 05:59:22

标签: ios swift uistackview

我有一个用于自定义UITabBar的自定义UIView,其高度固定为50,我为每个按钮添加了一个stackview,但我需要中间按钮的高度应该更大。

let view: UIView = views[3]
view.heightAnchor.equalToConstant(100.0).isActive = true

let stackView: UIStackView = UIStackView(arrangedSubviews: [views])
     stackView.translatesAutoresizingMaskIntoConstraints = false
     stackView.axis = .horizontal
     stackView.distribution = .fillEqually
     addSubview(stackView)
     stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
     stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
     stackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
     stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true

如何更改stackView中特定视图的高度?

2 个答案:

答案 0 :(得分:0)

我可能无法理解您的问题,但是您可以像以前一样使用约束条件来更改任何视图的高度。 当视图是stackview的子视图时,也是如此。 Stackview更新其大小以满足其约束。 您必须在stackview中引用视图的高度限制。 然后,您可以在需要时进行更改。 同样,最好将stackviews alignment属性设置为具有所需的外观。

let heightConstraint = view.heightAnchor.equalToConstant(100.0)
heightConstraint.isActive = true
.
.
.
heightConstraint.constant = 150

答案 1 :(得分:0)

您要更改堆栈视图的Alignment属性...

假设5个视图的每个高度限制为60,并且您希望第四个视图(views[3])的高度为100。

StackView对齐方式Top

enter image description here

StackView对齐方式Center

enter image description here

StackView对齐方式Bottom

enter image description here

相关问题