title: How can I use Autolayout to set constraints on my UIScrollview?
body: <p>I have spent two days trying out the various solutions for <a href="https://developer.apple.com/library/ios/technotes/tn2154/_index.html#//apple_ref/doc/uid/DTS40013309" rel="noreferrer">Mixed and Pure Autolayout approaches</a> to achieve what was a trivial scrollview setup prior to autolayout, and it's now official - I must be too stupid. I am setting this up mostly in Storyboard (well, it's just the way it is).</p>
  
  <p>So here's my plea for help.</p>
  
  <p>Viewtree:</p>
  
  <pre><code>UIView
  -UIView
  -UIView
  ..-UIScrollview
  ...-UIButton
  ...-UIButton
  ...-UIButton
  </code></pre>
  
  <p>The buttons are supposed to scroll horizontally (left to right and vice versa). Can someone <em>please</em> let me know how to set the constraints to achieve this using pure Autolayout???</p>
  
  <p>--</p>
  
  <p>I have tried the mixed approach, like so:</p>
  
  <pre><code>UIView
  - UIView
  - UIView
  ..-UIScrollview
  ...-UIView (contentview)
  ....-UIButton
  ....-UIButton
  ....-UIButton
  </code></pre>
  
  <p>...and setting fixed width and height constraints for the <code>contentview</code> and the <code>translatesAutoresizingMaskIntoConstraints</code> settings as per Apple's TechNote. The buttons and scrollview are set up using constraints. This gets the scrollview scrolling (yay) but alas, it scrolls too far! As far as I can tell, the scroll width is somehow doubled from what I set the contentview at???!!!???</p>
  
  <p>I tried the pure autolayout approach as well, both with <code>contentview</code> and without. <strong>All</strong> the views are <code>translatesAutoresizingMaskIntoConstraints=NO</code>, except for <code>self.view</code>. The buttons have fixed width/height constraints, and are pinned to all four edges of the scrollview. Nothing scrolls.</p>
  
  <p>So I am totally baffled why I can't get it to work correctly. Any help is much appreciated, and if you need any other info, please ask!</p>
  
  <p><strong>UPDATED Screenshot with solution -</strong>
  buttonZ constraints:</p>
  
  <p><img src="https://i.stack.imgur.com/fdkm8.png" alt="enter image description here"></p>
  
  <p><strong>EDIT @ Jamie Forrest</strong>
  So the solution turns out to be the wrong trailing constraint on the last button. Instead of 6441, the value I had set was negative, -6441. The tricky thing is, that when setting the value in storyboard, there are two options in the Pin toolbar:</p>
  
  <p><img src="https://i.stack.imgur.com/18dfp.png" alt="enter image description here"></p>
  
  <p>The Current Canvas Value is negative (leading to no scroll), and the option below is positive (activating scroll). This means I'm not stupid but at least half-blind I guess. <em>Although, to my defense, isn't it somewhat disturbing that XCode doesn't show an error for the "incorrect" setting?</em></p>
  
  <p><strong>EDITED AGAIN</strong>
  Now this is funny... changing the trailing value from -6441 (no scroll) to 6441 enabled scroll. But my old friend the "too much contentsize" was back, leading to a content size twice as large as what it should be! The solution to get the correct content scroll was to set the trailing constraint to ZERO! This is not obvious when working in Storyboard but looking at @Infinity James' code, it is what it should be.</p>