Valysar
Valysar

Reputation: 61

UIStackview not resizing when hiding the labels inside it

Hi I'm new to UIStackViews.
So I have a vertical Stackview containing 8 Labels.
However, the problem with my Stackview is that whenever I hide the Labels inside it, my stackview does not resize.

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

How do I fix this?
Here's how I hid the labels:

 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

Thanks

Upvotes: 3

Views: 5160

Answers (2)

Hitesh Agarwal
Hitesh Agarwal

Reputation: 2015

Just hide the labels works. You need to set constraints to make it work. I upload images of constraints.

enter image description here

stackview's constraint

enter image description here

Upvotes: 1

user2621857
user2621857

Reputation: 51

hiding a label inside stack view does not effect the height. I suggest that, when you want to show some detail (stackView contain some UILabel),

create the UILabel e.g:

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

then add the UILabelView to the stack view

stackViewName.addArrangedSubview(label)

at the end, instead of hide a label inside stack view, you have to remove the UILabel from the stack view

stackView.removeArrangedSubview(label)

so i think this will effect the hight of your stackView .

Upvotes: 0

Related Questions