title: Programatically Creating a Stack of UILabels
body: <p>I was messing around with <a href="https://gist.github.com/twostraws/a02d4cc09fc7bc16859c" rel="nofollow noreferrer">this</a> example of a programatically created scroll view and stack view, and I decided to experiment and change the UIButtons to UILabels. I replaced the code inside of the for loop with this code:</p>
  
  <pre><code>let label = UILabel()
  label.text = "Label"
  stackView.addArrangedSubview(label)
  </code></pre>
  
  <p>When I re-ran the app, however, I found that the scroll view could no longer be scrolled. After debugging, I found that the stack view's <code>frame</code> had zero width and zero height, which I presume to be the source of the problem. I've been unable to figure out why the stack view has no width or height or how to make the scroll view scroll once again.</p>
  
  <p>Here's the full view controller, with my modifications to compile it for Swift 4, use UILabel, and print the frame size:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      var scrollView: UIScrollView!
      var stackView: UIStackView!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          scrollView = UIScrollView()
          scrollView.translatesAutoresizingMaskIntoConstraints = false
          view.addSubview(scrollView)
  
          view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
          view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
  
  
          stackView = UIStackView()
          stackView.translatesAutoresizingMaskIntoConstraints = false
          stackView.axis = .vertical
          scrollView.addSubview(stackView)
  
          scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[stackView]|", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))
          scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[stackView]", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))
  
          for _ in 1 ..&lt; 100 {
              let label = UILabel()
              label.text = "Label"
              label.sizeToFit()
              stackView.addArrangedSubview(label)
  
  //            let vw = UIButton(type: UIButtonType.system)
  //            vw.setTitle("Button", for: .normal)
  //            stackView.addArrangedSubview(vw)
          }
      }
  
      override func viewDidLayoutSubviews() {
          super.viewDidLayoutSubviews()
          scrollView.contentSize = CGSize(width: stackView.frame.width, height: stackView.frame.height)
          print("stack view frame: \(stackView.frame)")
      }
  
  }
  </code></pre>