Advertisement
Don_Mag

StackView show/hide arranged subview

Aug 25th, 2021
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.00 KB | None | 0 0
  1. class StackViewController: UIViewController {
  2.    
  3.     let stackView: UIStackView = {
  4.         let v = UIStackView()
  5.         v.axis = .vertical
  6.         return v
  7.     }()
  8.    
  9.     override func viewDidLoad() {
  10.         super.viewDidLoad()
  11.        
  12.         // two views to add to the stack view
  13.         let v1 = UIView()
  14.         let v2 = UIView()
  15.        
  16.         // a "container" for the stack view
  17.         let stackContainer = UIView()
  18.        
  19.         v1.backgroundColor = .red
  20.         v2.backgroundColor = .green
  21.         stackContainer.backgroundColor = .yellow
  22.        
  23.         stackView.translatesAutoresizingMaskIntoConstraints = false
  24.         stackContainer.translatesAutoresizingMaskIntoConstraints = false
  25.        
  26.         stackView.addArrangedSubview(v1)
  27.         stackView.addArrangedSubview(v2)
  28.        
  29.         stackContainer.addSubview(stackView)
  30.         view.addSubview(stackContainer)
  31.        
  32.         let g = view.safeAreaLayoutGuide
  33.        
  34.         NSLayoutConstraint.activate([
  35.  
  36.             // stack view "container" view Top: 40 / centerX
  37.             stackContainer.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
  38.             stackContainer.centerXAnchor.constraint(equalTo: g.centerXAnchor),
  39.            
  40.             // stackView all 4 sides to "container" view
  41.             stackView.topAnchor.constraint(equalTo: stackContainer.topAnchor),
  42.             stackView.leadingAnchor.constraint(equalTo: stackContainer.leadingAnchor),
  43.             stackView.trailingAnchor.constraint(equalTo: stackContainer.trailingAnchor),
  44.             stackView.bottomAnchor.constraint(equalTo: stackContainer.bottomAnchor),
  45.            
  46.             // both arranged subviews 240x160
  47.             v1.widthAnchor.constraint(equalToConstant: 240.0),
  48.             v1.heightAnchor.constraint(equalToConstant: 160.0),
  49.            
  50.             v2.widthAnchor.constraint(equalTo: v1.widthAnchor),
  51.             v2.heightAnchor.constraint(equalTo: v1.heightAnchor),
  52.            
  53.         ])
  54.        
  55.         let t = UITapGestureRecognizer(target: self, action: #selector(gotTap(_:)))
  56.         view.addGestureRecognizer(t)
  57.        
  58.     }
  59.    
  60.     @objc func gotTap(_ sender: Any?) -> Void {
  61.        
  62.         // get a reference to the 2nd arranged subview
  63.         guard let v = stackView.arrangedSubviews.last else {
  64.             return
  65.         }
  66.        
  67.         // hide / show
  68.         v.isHidden.toggle()
  69.        
  70.     }
  71.    
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement