【问题标题】:UIStackview not resizing when hiding the labels inside itUIStackview 隐藏标签时不调整大小
【发布时间】:2018-07-22 03:41:07
【问题描述】:

您好,我是 UIStackViews 的新手。
所以我有一个包含 8 个标签的垂直 Stackview。
但是,我的 Stackview 的问题是,每当我将标签隐藏在其中时,我的 stackview 都不会调整大小。

The labels within the Red Rectangle are in my Stackview
When I hide those labels of the Stackview, I want my screen to look like this
However for some reason when I hide those labels, it looks like this instead with all the space visible and the stuff at the bottom doesn't go up

我该如何解决这个问题?
以下是我隐藏标签的方式:

 override func viewDidLoad() {
        super.viewDidLoad()
        showingResultLabel.isHidden = true
        addressLabel.isHidden = true
        costOfKitLabel.isHidden = true
        costOfModularLabel.isHidden = true
        dispatchedFromLabel.isHidden = true
        kitHomecostLabel.isHidden = true
        modularHomecostLabel.isHidden = true
        dispatchFromLabel.isHidden = true

谢谢

【问题讨论】:

  • 看起来你必须隐藏红色区域并向上移动橙色区域。和堆栈视图中的红色区域,但橙色区域不在堆栈视图中。对吗??
  • @HiteshAgarwal 没错!
  • 检查约束是否将固定高度设置为 stackview ??
  • 嗨,我的 stackview 没有限制
  • 可能是您将约束设置为底部标签,它不会让它上升。您是否将底部约束设置为等于“估计...”标签

标签: ios swift xcode uistackview


【解决方案1】:

只需隐藏标签即可。您需要设置约束以使其工作。我上传约束图像。

stackview 的约束

【讨论】:

    【解决方案2】:

    在堆栈视图中隐藏标签不会影响高度。 我建议,当你想显示一些细节(stackView 包含一些 UILabel)时,

    创建 UILabel 例如:

     let label = UILabel()
       label.textColor = .black
       label.text = ""
       label.backgroundColor = .clear
    

    然后将 UILabelView 添加到堆栈视图中

    stackViewName.addArrangedSubview(label)
    

    最后,您必须从堆栈视图中删除 UILabel,而不是在堆栈视图中隐藏标签

    stackView.removeArrangedSubview(label)
    

    所以我认为这会影响您的 stackView 的高度。

    【讨论】:

    • " 首先将参数添加到 UILabel 然后将 UILabelView 添加到堆栈视图" 对不起这是什么意思
    • 好吧,我试过了,但它仍然没有把底部的东西推到顶部
    • ok 那么,你可以在移除对象stackView.frame.size.height = 0后静态设置stackView高度
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 2018-04-26
    相关资源
    最近更新 更多