Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class StackViewController: UIViewController {
- let stackView: UIStackView = {
- let v = UIStackView()
- v.axis = .vertical
- return v
- }()
- override func viewDidLoad() {
- super.viewDidLoad()
- // two views to add to the stack view
- let v1 = UIView()
- let v2 = UIView()
- // a "container" for the stack view
- let stackContainer = UIView()
- v1.backgroundColor = .red
- v2.backgroundColor = .green
- stackContainer.backgroundColor = .yellow
- stackView.translatesAutoresizingMaskIntoConstraints = false
- stackContainer.translatesAutoresizingMaskIntoConstraints = false
- stackView.addArrangedSubview(v1)
- stackView.addArrangedSubview(v2)
- stackContainer.addSubview(stackView)
- view.addSubview(stackContainer)
- let g = view.safeAreaLayoutGuide
- NSLayoutConstraint.activate([
- // stack view "container" view Top: 40 / centerX
- stackContainer.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
- stackContainer.centerXAnchor.constraint(equalTo: g.centerXAnchor),
- // stackView all 4 sides to "container" view
- stackView.topAnchor.constraint(equalTo: stackContainer.topAnchor),
- stackView.leadingAnchor.constraint(equalTo: stackContainer.leadingAnchor),
- stackView.trailingAnchor.constraint(equalTo: stackContainer.trailingAnchor),
- stackView.bottomAnchor.constraint(equalTo: stackContainer.bottomAnchor),
- // both arranged subviews 240x160
- v1.widthAnchor.constraint(equalToConstant: 240.0),
- v1.heightAnchor.constraint(equalToConstant: 160.0),
- v2.widthAnchor.constraint(equalTo: v1.widthAnchor),
- v2.heightAnchor.constraint(equalTo: v1.heightAnchor),
- ])
- let t = UITapGestureRecognizer(target: self, action: #selector(gotTap(_:)))
- view.addGestureRecognizer(t)
- }
- @objc func gotTap(_ sender: Any?) -> Void {
- // get a reference to the 2nd arranged subview
- guard let v = stackView.arrangedSubviews.last else {
- return
- }
- // hide / show
- v.isHidden.toggle()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement