How to hide elements in a Stack View
Asked Answered
B

2

7

I have 4 separate views and I want to hide the other 3 of them when one of the buttons is pressed.

I have them in a UIStackView but .isHidden = true does not hide the views for some reason.

It works fine when they're not in a stack view.

@IBAction func qbpressed(_ sender: Any) {
    QBContainer.isHidden = false
    WRContainer.isHidden = true
    RBContainer.isHidden = true
    QBIndicator.isHidden = false
    WRIndicator.isHidden = true
    RBIndicator.isHidden = true
    TEIndicator.isHidden = true
    QBButton.setTitleColor(#colorLiteral(red: 0, green: 0.5008062124, blue: 1, alpha: 1), for: .normal)
    WRButton.setTitleColor(#colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1), for: .normal)
    RBButton.setTitleColor(#colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1), for: .normal)
    TEButton.setTitleColor(#colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1), for: .normal)

    if intersitial.isReady{
        intersitial.present(fromRootViewController: self)
    } 
}

stack view

Burgwell answered 13/8, 2018 at 20:51 Comment(3)
isHidden should work. You may need show post your codePeay
just posted, isHidden works when the views are not in a stack view, but does not work when I put them in a stack viewBurgwell
Either the properties (QBContainer, WRContainer, etc.) are not connected to those views, or something is setting the isHidden properties back to true. Use the debugger to figure out which.Antisepticize
B
16

isHidden property doesn't work, but you can use alpha and achieve the same result,

QBIndicator.alpha = 1.0 will work for QBIndicator.isHidden = false and QBIndicator.alpha = 0.0 will work for QBIndicator.isHidden = true

Burgwell answered 14/8, 2018 at 0:17 Comment(2)
every API apple writes you find a bug making a nest and raising a family, because that crap will never be solved. StackViews have a decade.Celloidin
note - if you've set the stackView to fill equally, if you do isHidden = true, it will reorganize the subviews to fill all the stackView's place, equally. If you do alpha = 0, it will just hide the subview, but other subviews will stay on their place.Eoin
A
13

setting a view to hidden should make it no longer visible, regardless of whether or not it inside a UIStackView.

The benefit of UIStackView is that it provides free animation through the isHidden property, like so:

// Assuming stackViewSubView.isHidden == false here
UIView.animate(withDuration: 0.25, animations: {
    self.stackViewSubView.isHidden = true
    self.view.layoutIfNeeded()
})
Altair answered 13/8, 2018 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.