title: Showing and Hiding a container UIView with its subviews programmatically
body: <p>I've got a label and a button side by side as shown as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/lY3hA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lY3hA.png" alt="enter image description here"></a></p>
  
  <p>This is created by the following code:</p>
  
  <pre><code>private func addViewBack() {
      myLabel.translatesAutoresizingMaskIntoConstraints = false
      myLabel.text = "Show my label"
  
      myButton.addTarget(self, action: #selector(pressed), for: .touchUpInside)
      myButton.setTitleColor(UIColor.lightGray, for: UIControl.State.normal)
      myButton.translatesAutoresizingMaskIntoConstraints = false
      myButton.setTitle("Button", for: .normal)
      myButton.clipsToBounds = true
  
      containerView.backgroundColor = .red
      containerView.addSubview(myLabel)
      containerView.translatesAutoresizingMaskIntoConstraints = false
      containerView.addSubview(myButton)
  
      containerView.addConstraint(NSLayoutConstraint(item: myButton, attribute: .centerY, relatedBy: .equal, toItem: myLabel, attribute: .centerY, multiplier: 1, constant: 0))
      containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-8-[myLabel]-8-|", options: [], metrics: nil, views: ["myLabel":myLabel]))
      containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[myLabel]", options: [], metrics: nil, views: ["myLabel":myLabel]))
      containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[myButton]-8-|", options: [], metrics: nil, views: ["myButton":myButton]))
      containerView.layoutIfNeeded()
  
      self.view.addSubview(containerView)
  
      self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[topView]-100-[containerView]", options: [], metrics: nil, views: ["containerView":containerView,"topView":viewTop]))
      self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[containerView]-8-|", options: [], metrics: nil, views: ["containerView":containerView]))
      self.view.layoutIfNeeded()
  }
  </code></pre>
  
  <p>I've got show and hide buttons that are supposed to show and hide the container view when pressing them as follows:</p>
  
  <pre><code>@IBAction func show(_ sender: Any) {
  
      if let constraint = (containerView.constraints.filter{$0.firstAttribute == .height}.first) {
          constraint.isActive = false
      }
  
      containerView.layoutIfNeeded()
  }
  
  @IBAction func hide(_ sender: Any) {
      containerView.heightAnchor.constraint(equalToConstant: 0).isActive = true
      containerView.layoutIfNeeded()
  
  }
  </code></pre>
  
  <p>When using the hide action, the button is never hidden and I end up with something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/mn6Se.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mn6Se.png" alt="enter image description here"></a></p>
  
  <p>When I add the following it works:</p>
  
  <pre><code>@IBAction func hide(_ sender: Any) {
      containerView.heightAnchor.constraint(equalToConstant: 0).isActive = true
      myButton.heightAnchor.constraint(equalToConstant: 0).isActive = true
      containerView.layoutIfNeeded()
  
  }
  </code></pre>
  
  <p>When I press the show action, the button never reappears again like so:</p>
  
  <p><a href="https://i.stack.imgur.com/FyHYL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FyHYL.png" alt="enter image description here"></a></p>
  
  <p>How can I very simply show and hide elements in a container view using auto layout programmatically? </p>