title: UIScrollView with iOS Auto Layout Constraints: Wrong size for subviews
body: <p>I'm trying to generate a view in code. Here's the hierachy of my view object</p>
  
  <ul>
  <li>UIScrollView
  
  <ul>
  <li>UIView
  
  <ul>
  <li>UIButton</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>The ScrollView should be the same size as the window.
  The button should be as big as possible.
  I'm using iOS auto layout, so the constraint strings for all of my objects look like this</p>
  
  <pre><code>H:|[object]|
  V:|[object]|
  </code></pre>
  
  <p>I've also set <code>translatesAutoresizingMaskIntoConstraints</code> to <code>NO</code> for each object.</p>
  
  <p>The problem is that the button only gets the default button-size. Its parent view object (UIView) only gets the size its subviews need. </p>
  
  <p><img src="https://i.stack.imgur.com/JSmsq.png" alt="enter image description here"></p>
  
  <p><em>red: UIScrollView / yellow: UIView</em></p>
  
  <p>How can I force those views to be as big as the scrollView?</p>
  
  <p>When I use a UIView instead of th UIScrollView everything works great...</p>
  
  <p>Here's some code:</p>
  
  <pre><code>    - (void) viewDidLoad {
  
          [super viewDidLoad];
  
          // SCROLL VIEW
          UIScrollView* scrollView = [UIScrollView new];
          scrollView.backgroundColor=[UIColor redColor];
          scrollView.translatesAutoresizingMaskIntoConstraints = NO;
  
          //CONTAINER VIEW
          UIView *containerView = [UIView new];
          containerView.translatesAutoresizingMaskIntoConstraints = NO;
          containerView.backgroundColor = [UIColor yellowColor];
          [scrollView addSubview:containerView];
  
          // CONSTRAINTS SCROLL VIEW - CONTAINER VIEW
          [scrollView addConstraints:
           [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|"
                                                   options:0 metrics:nil
                                                     views:@{@"containerView":containerView}]];
          [scrollView addConstraints:
           [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|"
                                                   options:0 metrics:nil
                                                     views:@{@"containerView":containerView}]];
  
          // BUTTON
          UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          button.translatesAutoresizingMaskIntoConstraints = NO;
          [button setTitle:@"I'm way to small" forState:UIControlStateNormal];
          [containerView addSubview:button];
  
          // CONSTRAINTS CONTAINER VIEW - BUTTON
          [containerView addConstraints:
           [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button]|"
                                                   options:0 metrics:nil
                                                     views:@{@"button":button}]];
          [containerView addConstraints:
           [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|"
                                                   options:0 metrics:nil
                                                     views:@{@"button":button}]];
          self.view = scrollView;
  
      }
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  I really don't know, why this is happening. If you set up the view in IB, connect the outlets and instanciate the view in code, the scrollview behaves like a normal view (which bounces vertically). Its contentSize is not calculated correctly. More <a href="http://nscookbook.com/2013/01/recipe-6-creating-a-custom-uiview-using-a-nib/" rel="noreferrer">here</a>. But how to do it correctly?</p>