Advertisement
Don_Mag

Untitled

Nov 1st, 2022
1,624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.05 KB | None | 0 0
  1. //
  2.  
  3. class StackTestVC: UIViewController {
  4.    
  5.     let v1 = UILabel()
  6.     let v2 = UILabel()
  7.     let v3 = UILabel()
  8.    
  9.     override func viewDidLoad() {
  10.         super.viewDidLoad()
  11.        
  12.         view.backgroundColor = .systemYellow
  13.        
  14.         let containerView = UIView()
  15.        
  16.         let stackView = UIStackView()
  17.         stackView.axis = .horizontal
  18.         stackView.spacing = 0
  19.         stackView.distribution = .fillEqually
  20.        
  21.         containerView.translatesAutoresizingMaskIntoConstraints = false
  22.         stackView.translatesAutoresizingMaskIntoConstraints = false
  23.  
  24.         view.addSubview(containerView)
  25.         containerView.addSubview(stackView)
  26.        
  27.         let g = view.safeAreaLayoutGuide
  28.         NSLayoutConstraint.activate([
  29.  
  30.             containerView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  31.             containerView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  32.             containerView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  33.             containerView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
  34.  
  35.             stackView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0),
  36.             stackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0.0),
  37.             stackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0.0),
  38.  
  39.         ])
  40.  
  41.         let colors: [UIColor] = [
  42.             .systemGreen, .systemBlue, .systemOrange
  43.         ]
  44.         let labels: [UILabel] = [
  45.             v1, v2, v3
  46.         ]
  47.         var i: Int = 0
  48.         for (v, c) in zip(labels, colors) {
  49.             i += 1
  50.             v.text = "\(i)"
  51.             v.textAlignment = .center
  52.             v.textColor = .white
  53.             v.font = .systemFont(ofSize: 40.0, weight: .regular)
  54.             v.backgroundColor = c
  55.             v.heightAnchor.constraint(equalToConstant: 150.0).isActive = true
  56.             stackView.addArrangedSubview(v)
  57.         }
  58.    
  59.         containerView.backgroundColor = .white
  60.         containerView.layer.cornerRadius = 16
  61.         containerView.layer.borderColor = UIColor.white.cgColor
  62.         containerView.layer.borderWidth = 4
  63.        
  64.         containerView.layer.masksToBounds = true
  65.     }
  66.  
  67.     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  68.         v2.isHidden.toggle()
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement