// class StackTestVC: UIViewController { let v1 = UILabel() let v2 = UILabel() let v3 = UILabel() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemYellow let containerView = UIView() let stackView = UIStackView() stackView.axis = .horizontal stackView.spacing = 0 stackView.distribution = .fillEqually containerView.translatesAutoresizingMaskIntoConstraints = false stackView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(containerView) containerView.addSubview(stackView) let g = view.safeAreaLayoutGuide NSLayoutConstraint.activate([ containerView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0), containerView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0), containerView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0), containerView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0), stackView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0), stackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0.0), stackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0.0), ]) let colors: [UIColor] = [ .systemGreen, .systemBlue, .systemOrange ] let labels: [UILabel] = [ v1, v2, v3 ] var i: Int = 0 for (v, c) in zip(labels, colors) { i += 1 v.text = "\(i)" v.textAlignment = .center v.textColor = .white v.font = .systemFont(ofSize: 40.0, weight: .regular) v.backgroundColor = c v.heightAnchor.constraint(equalToConstant: 150.0).isActive = true stackView.addArrangedSubview(v) } containerView.backgroundColor = .white containerView.layer.cornerRadius = 16 containerView.layer.borderColor = UIColor.white.cgColor containerView.layer.borderWidth = 4 containerView.layer.masksToBounds = true } override func touchesBegan(_ touches: Set, with event: UIEvent?) { v2.isHidden.toggle() } }