title: iOs - insert uiview into another view in code, keep constraints
body: <p>What I'm trying to achieve.
  Whenever I have a UIViewController that inherits from my class SuperViewController, in viewDidLoad of my superview controller i take all the subviews of my UIViewController, create a UIScrollView, add all of the subviews to it and set this UIScrollView as the only child of UIViewController.</p>
  
  <p>UIViewController structure before calling [super viewDidLoad];</p>
  
  <pre><code>UIViewController
    UIView
       subview1
       subview2
       subview3
  </code></pre>
  
  <p>after</p>
  
  <pre><code>UIViewController
    UIView
      UIScrollView
         subview1
         subview2
         subview3
  </code></pre>
  
  <p>with the constraints that concern UIView - subviews, moved to UIScrollView-subviews</p>
  
  <p>The reason I'm doing this is because in our app we have a lot of dealing with scrollViews and I just want to write the code to add them once in the super class and have it enable/ disable scrolling based on the content size.</p>
  
  <p>What i have currently in my superviewController(simplified)</p>
  
  <pre><code> - (void)viewDidLoad
  {
      [super viewDidLoad];
  
       if([self class]!=[SuperViewController class] &amp;&amp; [self isKindOfClass:[SuperViewController class]])
       {
          //subclass called this method
           UIScrollView *scrollViewToAdd = [[UIScrollView alloc] init];
           UIView *originalView = self.view;
  
           NSArray* savedConstraints = self.view.constraints;
           for (UIView* view in self.view.subviews)
           {
              [scrollViewToAdd addSubview:view];
           }
  
           for (NSLayoutConstraint*oldConstr in savedConstraints)
           {
                 //loop through all of the constraints associated with the UIView. If one of the items is UIView, replace it with UIScrollView
                 //add all of these constraints to UIScrollView
                 NSLayoutConstraint *newConstraint = [NSLayoutConstraint    constraintWithItem:oldConstr.firstItem==self.view?scrollViewToAdd:oldConstr.firstItem  attribute:oldConstr.firstAttribute relatedBy:oldConstr.relation  toItem:oldConstr.secondItem==self.view?scrollViewToAdd:oldConstr.secondItem  attribute:oldConstr.secondAttribute multiplier:oldConstr.multiplier constant:oldConstr.constant];
                newConstraint.priority = oldConstr.priority;
                [scrollViewToAdd addConstraint:newConstraint];
  
          }
  
       }
       //moving the subviews sets self.view to nil, thankfully i kept a reference so i can set it to the original value
       _scrollView = scrollViewToAdd;
       self.view = originalView;
       //add scrollview as a child
       [self.view addSubview:_scrollView];
  
       //add scrollView constraints
       id topGuide =self.topLayoutGuide;
       id bottomGuide = self.bottomLayoutGuide;
  
      _scrollViewToTop =  [[NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-0-[_scrollView]"
                                                                options: 0
                                                                metrics: nil
                                                                  views: NSDictionaryOfVariableBindings (topGuide, _scrollView )] firstObject];
  
  
      _scrollViewToBottom =  [[NSLayoutConstraint constraintsWithVisualFormat: @"V:[_scrollView]-0-[bottomGuide]"
                                                                  options: 0
                                                                  metrics: nil
                                                                    views:      NSDictionaryOfVariableBindings (bottomGuide, _scrollView)] firstObject];
  
  
       [self.view addConstraint:_scrollViewToTop];
       [self.view addConstraint:_scrollViewToBottom];
  
  
  
  
   }
  </code></pre>
  
  <p>This gives me constraint conflicts I don't understand </p>
  
  <pre><code> (
  "&lt;_UILayoutSupportConstraint:0xb866db0 V:[_UILayoutGuide:0xf9b2750(0)]&gt;",
  "&lt;_UILayoutSupportConstraint:0xb866cf0 V:|-(0)-[_UILayoutGuide:0xf9b2750]   (Names: '|':UIView:0x9e90480 )&gt;",
  "&lt;_UILayoutSupportConstraint:0xb866f90 V:[_UILayoutGuide:0xb862070(0)]&gt;",
  "&lt;_UILayoutSupportConstraint:0xb866ec0 _UILayoutGuide:0xb862070.bottom == UIView:0x9e90480.bottom&gt;",
  "&lt;NSLayoutConstraint:0xb87a710 V:[_UILayoutGuide:0xf9b2750]-(0)-[UIScrollView:0x9e84b00]&gt;",
  "&lt;NSLayoutConstraint:0xb861e80 V:[UIScrollView:0x9e84b00]-(0)-[_UILayoutGuide:0xb862070]&gt;",
  "&lt;NSAutoresizingMaskLayoutConstraint:0xf9a5280 h=--&amp; v=--&amp; UIScrollView:0x9e84b00.midY ==&gt;",
  "&lt;NSAutoresizingMaskLayoutConstraint:0xf992950 h=-&amp;- v=-&amp;- UIView:0x9e90480.height == UIViewControllerWrapperView:0xb87bd20.height - 64&gt;",
  "&lt;NSAutoresizingMaskLayoutConstraint:0xb870f90 h=-&amp;- v=-&amp;- UIViewControllerWrapperView:0xb87bd20.height == UINavigationTransitionView:0x9a6e3e0.height&gt;",
  "&lt;NSAutoresizingMaskLayoutConstraint:0xf9b3560 h=-&amp;- v=-&amp;- UINavigationTransitionView:0x9a6e3e0.height == UILayoutContainerView:0x9a6dd10.height&gt;",
  "&lt;NSLayoutConstraint:0xb876390 'UIView-Encapsulated-Layout-Height' V:[UILayoutContainerView:0x9a6dd10(480)]&gt;"
  )
  
  Will attempt to recover by breaking constraint 
  &lt;NSLayoutConstraint:0xb861e80 V:[UIScrollView:0x9e84b00]-(0)-[_UILayoutGuide:0xb862070]&gt;
  </code></pre>
  
  <p>I'm able to remove this conflict if instead of </p>
  
  <pre><code>     self.view = originalView;
       [self.view addSubview:_scrollView];
  </code></pre>
  
  <p>I do</p>
  
  <pre><code>     [self.view = _scrollView];
  </code></pre>
  
  <p>but that doesn't result in the view hierarchy I want.</p>