title: UITableView top to topLayoutGuide programmatically content inset
body: <p>Due to my attempt to implement iAds "properly" (i.e., sharing a single instance of <code>ADBannerView</code>), I am creating a <code>UITableView</code> programatically inside a <code>UIViewController</code> and adding it to the view. Below are a couple of snippets from my subclass of <code>UIViewController</code>:</p>
  
  <p>From <code>viewDidLoad</code></p>
  
  <pre><code>self.tableView = UITableView(frame: self.view.bounds, style: .Grouped)
  self.tableView.allowsSelectionDuringEditing = true
  self.tableView.registerNib(UINib(nibName: "TableViewCellWithSwitch", bundle: nil), forCellReuseIdentifier: "SliderCellIdentifier")
  self.tableView.dataSource = self
  self.tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
  
  self.view.addSubview(self.tableView)
  self.tableViewBottomLayoutConstraint = NSLayoutConstraint(item: self.tableView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute: .Top, multiplier: 1, constant: 0)
  self.view.addConstraints([
      NSLayoutConstraint(item: self.tableView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 0),
      //NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0),
      //NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: self.topLayoutGuide.length),
      //NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0),
      NSLayoutConstraint(item: self.tableView, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant: 0),
      self.tableViewBottomLayoutConstraint
      ])
  // This must be called or the use of self.topLayoutGuide will not function
  // See: https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/topLayoutGuide
  self.view.layoutSubviews()
  </code></pre>
  
  <p>iAds implementation (added to try and justify, rightly or wrongly, my implementation of the <code>UITableView</code>)</p>
  
  <pre><code>func showiAds(animated: Bool) {
      println("Show iAd")
      if !self.showingiAd {
          println("Showing iAd")
          self.showingiAd = true
  
          // Add the banner view below the content before it's then animated in to view
          let delegate = UIApplication.sharedApplication().delegate as AppDelegate
          let bannerView = delegate.bannerView
          self.bannerBottomConstraint = NSLayoutConstraint(item: bannerView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute: .Top, multiplier: 1, constant: bannerView.frame.size.height)
  
          if (bannerView.superview != self.view) {
              bannerView.removeFromSuperview()
          }
          self.view.addSubview(bannerView)
          self.view.addConstraints([
              self.bannerBottomConstraint,
              NSLayoutConstraint(item: bannerView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 0),
              NSLayoutConstraint(item: bannerView, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant: 0),
              ])
          self.view.layoutIfNeeded()
  
  
          // Only the changing of the value of the top of the banner is animated so it "slides in" from the bottom
          self.bannerBottomConstraint.constant = 0
          self.view.setNeedsUpdateConstraints()
          UIView.animateWithDuration(animated ? 0.5 : 0, animations: { () -&gt; Void in
              // Calling layoutIfNeeded here will animate the layout constraint cosntant change made above
              self.view.layoutIfNeeded()
              }, completion: { (completed) -&gt; Void in
                  if completed {
                      println("Completed animation")
                  }
          })
      }
  }
  
  func hideiAds() {
      println("Hide iAd")
      if self.self.showingiAd {
          self.showingiAd = false
          println("Hiding iAd")
          let delegate = UIApplication.sharedApplication().delegate as AppDelegate
          let bannerView = delegate.bannerView
          if bannerView.superview == self.view {
              bannerView.removeFromSuperview()
          }
          self.view.removeConstraint(self.tableViewBottomLayoutConstraint)
          self.tableViewBottomLayoutConstraint = NSLayoutConstraint(item: self.tableView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute: .Top, multiplier: 1, constant: 0)
          self.view.addConstraint(self.tableViewBottomLayoutConstraint)
      }
  }
  </code></pre>
  
  <p>As you can see, there are 3 constraints commented out. Each of these seem to have different results. I won't post screenshots of them (unless requested), but I will describe them.</p>
  
  <p><code>NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0)</code></p>
  
  <p>iOS 7: Top of table and content of table is at the top of screen. Content is behind the navigation bar</p>
  
  <p>iOS 8: Top of table and content of table is below the navigation bar. Content is below the navigation bar (<strong>correct</strong>)</p>
  
  <p><code>NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: self.topLayoutGuide.length)</code></p>
  
  <p>iOS 7: Top of table and content of table is at the top of screen. Content is behind the navigation bar</p>
  
  <p>iOS 8: Top of table and content of table is below the navigation bar. Content is below the navigation bar (<strong>correct</strong>)</p>
  
  <p><code>NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0)</code></p>
  
  <p>iOS 7: Top of table and content of table is below the navigation bar. Content is below the navigation bar (<strong>correct</strong>)</p>
  
  <p>iOS 8: Top of table is at the bottom of the navigation bar (<strong>correct</strong>), but content of table is below the navigation bar, plus (what looks like) the height of the offset again (<strong>incorrect</strong>)</p>
  
  <p>I am aware that I could just do an <code>if iOS7 {...} else {...}</code>, but that feels pretty dirty and I've got a feeling it's my lack of understanding that's causing this issue, so I'd like to figure out how to have this work on iOS 7 and 8 without having resort to version checking, if possible.</p>