title: iOS Swift UIScrollView dynamic height
body: <p>I'm facing some problems while trying to calculate the height of a scrollView. I've made a "test" app to underline the problem.
  I just have a controller which is embedded in a NavigationController which is embedded itself in a TabBarController.</p>
  
  <p>Then I have a view on the top of the screen and a scrollView just under this view containing 2 labels. I'm trying to calculate the height of the scrollView by getting the biggest maxY among its subviews. Here is the code :</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      // I have to add this line so that the red view doesn't show under the navigation bar
      self.edgesForExtendedLayout = UIRectEdge.None
  
      let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 40))
      view.backgroundColor = UIColor.redColor()
  
      let scrollView = UIScrollView(frame: CGRect(x: 0, y: view.frame.maxY, width: self.view.frame.width, height: self.view.frame.height - view.frame.maxY))
      scrollView.backgroundColor = UIColor.yellowColor()
  
      let label1 = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
      label1.text = "test"
      label1.sizeToFit()
      scrollView.addSubview(label1)
  
      let label2 = UILabel(frame: CGRect(x: 0, y: 700, width: 0, height: 0))
      label2.text = "test"
      label2.sizeToFit()
      scrollView.addSubview(label2)
  
      // Calculate scrollView height
      var maxY:CGFloat = 0
      for view in scrollView.subviews {
          if view.frame.maxY &gt; maxY {
              maxY = view.frame.maxY
          }
      }
      scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: maxY)
  
      self.view.addSubview(view)
      self.view.addSubview(scrollView)
  }
  </code></pre>
  
  <p>So this is what I have:</p>
  
  <p><img src="https://s12.postimg.org/cw10d31r1/Screen_Shot_2015_12_07_at_11_18_54.png"/></p>
  
  <p>I should see the other "test" label at the bottom when scrolling but it's not the case, the scrollView is not big enough</p>
  
  <p>If I remove the red view, make the scrollView fits all the main view and remove this line
  <code>self.edgesForExtendedLayout = UIRectEdge.None</code>
  then it works :</p>
  
  <p><img src="https://s30.postimg.org/vgzzjxab5/Screen_Shot_2015_12_07_at_11_20_40.png"/></p>
  
  <p>Any solution ?</p>