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

pkamb
  • 33,281
  • 23
  • 160
  • 191
Noah Iarrobino
  • 1,435
  • 1
  • 10
  • 31
  • isHidden should work. You may need show post your code – ukim Aug 13 '18 at 20:55
  • just posted, isHidden works when the views are not in a stack view, but does not work when I put them in a stack view – Noah Iarrobino Aug 13 '18 at 21:29
  • 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. – rob mayoff Aug 13 '18 at 21:36

2 Answers2

14

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

Noah Iarrobino
  • 1,435
  • 1
  • 10
  • 31
  • 1
    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. – Duck Jun 24 '20 at 15:25
11

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()
})
sawarren
  • 286
  • 1
  • 5