Advertisement
Don_Mag

Stack View Distributions

Jul 5th, 2023
1,272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.66 KB | None | 0 0
  1.  
  2. class StackVC: UIViewController {
  3.  
  4.     var noHeightSVs: [UIStackView] = []
  5.     var withHeightSVs: [UIStackView] = []
  6.  
  7.     let bottomText: String = "Another label with different content"
  8.    
  9.     var topTexts: [String] = [
  10.         "This is a long label with dynamic content",
  11.         "Here is even more text being changed after the initial display to show how it automatically resizes",
  12.     ]
  13.    
  14.     override func viewDidLoad() {
  15.         super.viewDidLoad()
  16.  
  17.         view.backgroundColor = .systemBackground
  18.  
  19.         let labelColors: [UIColor] = [.cyan, .green,]
  20.  
  21.         let topText: String = topTexts.removeFirst()
  22.         topTexts.append(topText)
  23.        
  24.         var sv: UIStackView!
  25.         for _ in 0..<5 {
  26.             sv = UIStackView()
  27.             sv.axis = .vertical
  28.             sv.spacing = 10
  29.             sv.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
  30.             sv.heightAnchor.constraint(equalToConstant: 300.0).isActive = true
  31.             for (str, clr) in zip([topText, bottomText], labelColors) {
  32.                 let label = UILabel()
  33.                 label.numberOfLines = 0
  34.                 label.backgroundColor = clr
  35.                 label.text = str
  36.                 sv.addArrangedSubview(label)
  37.             }
  38.             withHeightSVs.append(sv)
  39.  
  40.             sv = UIStackView()
  41.             sv.axis = .vertical
  42.             sv.spacing = 10
  43.             sv.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
  44.             // NO height anchor
  45.             for (str, clr) in zip([topText, bottomText], labelColors) {
  46.                 let label = UILabel()
  47.                 label.numberOfLines = 0
  48.                 label.backgroundColor = clr
  49.                 label.text = str
  50.                 sv.addArrangedSubview(label)
  51.             }
  52.             noHeightSVs.append(sv)
  53.         }
  54.        
  55.         let g = view.safeAreaLayoutGuide
  56.        
  57.         let instruction = UILabel()
  58.         instruction.font = .italicSystemFont(ofSize: 20.0)
  59.         instruction.text = "Tap anywhere to toggle top label text."
  60.         instruction.translatesAutoresizingMaskIntoConstraints = false
  61.         view.addSubview(instruction)
  62.         NSLayoutConstraint.activate([
  63.             instruction.topAnchor.constraint(equalTo: g.topAnchor, constant: 100.0),
  64.             instruction.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 60.0),
  65.         ])
  66.  
  67.         let titleLabelW = UILabel()
  68.         titleLabelW.text = "Stack Views WITH height constraints:"
  69.         titleLabelW.textColor = .blue
  70.        
  71.         let titleLabelN = UILabel()
  72.         titleLabelN.text = "Stack Views WITHOUT height constraints:"
  73.         titleLabelN.textColor = .blue
  74.  
  75.         [titleLabelW, titleLabelN].forEach { v in
  76.             v.translatesAutoresizingMaskIntoConstraints = false
  77.             view.addSubview(v)
  78.         }
  79.        
  80.         titleLabelW.topAnchor.constraint(equalTo: instruction.bottomAnchor, constant: 40.0).isActive = true
  81.         titleLabelW.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 60.0).isActive = true
  82.  
  83.         var prevView: UIStackView!
  84.  
  85.         prevView = nil
  86.         withHeightSVs.forEach { v in
  87.             v.backgroundColor = .yellow
  88.             v.translatesAutoresizingMaskIntoConstraints = false
  89.             view.addSubview(v)
  90.             v.topAnchor.constraint(equalTo: titleLabelW.bottomAnchor, constant: 40.0).isActive = true
  91.             if prevView == nil {
  92.                 v.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 60.0).isActive = true
  93.             } else {
  94.                 v.leadingAnchor.constraint(equalTo: prevView.trailingAnchor, constant: 20.0).isActive = true
  95.             }
  96.             prevView = v
  97.         }
  98.  
  99.         titleLabelN.topAnchor.constraint(equalTo: withHeightSVs[0].bottomAnchor, constant: 60.0).isActive = true
  100.         titleLabelN.leadingAnchor.constraint(equalTo: titleLabelW.leadingAnchor, constant: 0.0).isActive = true
  101.        
  102.         prevView = nil
  103.         noHeightSVs.forEach { v in
  104.             v.backgroundColor = .yellow
  105.             v.translatesAutoresizingMaskIntoConstraints = false
  106.             view.addSubview(v)
  107.             v.topAnchor.constraint(equalTo: titleLabelN.bottomAnchor, constant: 40.0).isActive = true
  108.             if prevView == nil {
  109.                 v.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 60.0).isActive = true
  110.             } else {
  111.                 v.leadingAnchor.constraint(equalTo: prevView.trailingAnchor, constant: 20.0).isActive = true
  112.             }
  113.             prevView = v
  114.         }
  115.  
  116.         let dists: [UIStackView.Distribution] = [
  117.             .fill, .equalSpacing, .equalCentering, .fillEqually, .fillProportionally,
  118.         ]
  119.         for (sv, d) in zip(withHeightSVs, dists) {
  120.             sv.distribution = d
  121.         }
  122.         for (sv, d) in zip(noHeightSVs, dists) {
  123.             sv.distribution = d
  124.         }
  125.        
  126.         let infos: [String] = [
  127.             ".fill", ".equalSpacing", ".equalCentering", ".fillEqually", ".fillProportionally",
  128.         ]
  129.  
  130.         for (str, sv) in zip(infos, withHeightSVs) {
  131.             let label = UILabel()
  132.             label.font = .systemFont(ofSize: 16, weight: .bold)
  133.             label.textColor = .red
  134.             label.textAlignment = .center
  135.             label.text = str
  136.             label.translatesAutoresizingMaskIntoConstraints = false
  137.             view.addSubview(label)
  138.             NSLayoutConstraint.activate([
  139.                 label.leadingAnchor.constraint(equalTo: sv.leadingAnchor),
  140.                 label.trailingAnchor.constraint(equalTo: sv.trailingAnchor),
  141.                 label.bottomAnchor.constraint(equalTo: sv.topAnchor, constant: -8.0),
  142.             ])
  143.         }
  144.         for (str, sv) in zip(infos, noHeightSVs) {
  145.             let label = UILabel()
  146.             label.font = .systemFont(ofSize: 16, weight: .bold)
  147.             label.textColor = .red
  148.             label.textAlignment = .center
  149.             label.text = str
  150.             label.translatesAutoresizingMaskIntoConstraints = false
  151.             view.addSubview(label)
  152.             NSLayoutConstraint.activate([
  153.                 label.leadingAnchor.constraint(equalTo: sv.leadingAnchor),
  154.                 label.trailingAnchor.constraint(equalTo: sv.trailingAnchor),
  155.                 label.bottomAnchor.constraint(equalTo: sv.topAnchor, constant: -8.0),
  156.             ])
  157.         }
  158.        
  159.     }
  160.    
  161.     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  162.        
  163.         let topText: String = topTexts.removeFirst()
  164.         topTexts.append(topText)
  165.  
  166.         withHeightSVs.forEach { sv in
  167.             if let v = sv.arrangedSubviews.first as? UILabel {
  168.                 v.text = topText
  169.             }
  170.         }
  171.         noHeightSVs.forEach { sv in
  172.             if let v = sv.arrangedSubviews.first as? UILabel {
  173.                 v.text = topText
  174.             }
  175.         }
  176.     }
  177.    
  178. }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement