【问题标题】:How can I change height of the view which is present in StackView如何更改 StackView 中存在的视图的高度
【发布时间】:2019-10-24 05:59:22
【问题描述】:

我有一个自定义 UIView 用于自定义 UITabBar,其高度固定为 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 中特定视图的高度?

【问题讨论】:

    标签: ios swift uistackview


    【解决方案1】:

    我可能不理解您的问题,但您可以使用约束更改任何视图的高度,就像您已经完成的那样。 当视图是堆栈视图的子视图时也是如此。 Stackview 更新它的大小以满足它的约束。 您必须参考 stackview 中视图的高度约束。 然后,您可以随时更改它。 此外,最好将 stackviews alignment 属性设置为具有所需外观。

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

    【讨论】:

      【解决方案2】:

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

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

      StackView 对齐Top:

      StackView 对齐Center:

      StackView 对齐Bottom:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-23
        • 1970-01-01
        • 2015-09-07
        • 1970-01-01
        • 2013-02-22
        • 1970-01-01
        相关资源
        最近更新 更多