title: Programmatically resize custom UIView from XIB
body: <p>I have following problem: I have created a custom <code>UIView</code> class and its layout in <code>XIB</code> file. Let's say that size of my custom view in XIB is 150 x 50. I have enabled <code>sizeClasses</code> (wAny hAny) and <code>AutoLayout</code>. In Simulated Metrics I have set <code>Size = Freeform</code>, <code>Status Bar = None</code>, all <code>other = Inferred</code>.</p>
  
  <p>Code from my custom <code>UIView</code> class looks like this:</p>
  
  <pre><code>#import "CustomView.h"
  
  @implementation CustomView
  
  #pragma mark - Object lifecycle
  
  - (id)initWithFrame:(CGRect)frame {
      self = [super initWithFrame:frame];
  
      if (self) {
          [self setup];
      }
  
      return self;
  }
  
  - (id)initWithCoder:(NSCoder *)aDecoder {
      self = [super initWithCoder:aDecoder];
  
      if (self) {
          [self setup];
      }
  
      return self;
  }
  
  #pragma mark - Private &amp; helper methods
  
  - (void)setup {
      if (self.subviews.count == 0) {
          [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
          self.bounds = self.view.bounds;
          [self addSubview:self.view];
      }
  }
  
  @end
  </code></pre>
  
  <p>In my <code>UIViewController</code> where I want to add this custom <code>UIView</code>, I have made dummy UIView (which size I have set in Interface Builder) where I want to add my <code>CustomView</code> and <strong>I would like that my <code>CustomView</code> fits the bounds of my container view</strong>.</p>
  
  <p>I am trying to do that like this:</p>
  
  <pre><code>_vCustomView = [[CustomView alloc] init];
  _vCustomView.translatesAutoresizingMaskIntoConstraints = NO;
  [_vContainer addSubview:_vCustomView];
  
  [_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
  [_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
  [_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  [_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
  </code></pre>
  
  <p>But no luck. Let's say that my container view is 300 x 100, when I add my custom <code>UIView</code> to it, <strong>my custom view is still 150 x 50</strong>.</p>
  
  <p>I have tried to work without container view - to add my custom <code>UIView</code> object directly to self.view of my <code>UIViewController</code> and set it's width and height with:</p>
  
  <ul>
  <li><code>autoLayout</code> constraints</li>
  <li>by using <code>initWithFrame</code> when initializing my custom <code>UIView</code></li>
  </ul>
  
  <p>but again - no luck. Custom view is always 150 x 50.</p>
  
  <p>Can someone explain to me how can I <strong>programmatically add my custom <code>UIView</code> made in <code>XIB</code> and resize it using <code>autoLayout</code> constraints</strong>?</p>
  
  <p>Many thanks in advance.</p>
  
  <p><strong>Edit #1: Error I get when trying to run Andrea's code on my CustomView</strong></p>
  
  <pre><code>Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
  (
      "&lt;NSLayoutConstraint:0x17489b760 V:[UIView:0x17418d820(91)]&gt;",
      "&lt;NSLayoutConstraint:0x170c9bf80 V:|-(0)-[CustomView:0x1741da7c0]   (Names: '|':CustomView:0x1741da8b0 )&gt;",
      "&lt;NSLayoutConstraint:0x170c9bfd0 V:[CustomView:0x1741da7c0]-(0)-|   (Names: '|':CustomView:0x1741da8b0 )&gt;",
      "&lt;NSLayoutConstraint:0x170c9be90 V:|-(0)-[CustomView:0x1741da8b0]   (Names: '|':UIView:0x17418d820 )&gt;",
      "&lt;NSLayoutConstraint:0x170c9bee0 CustomView:0x1741da8b0.bottom == UIView:0x17418d820.bottom&gt;",
      "&lt;NSAutoresizingMaskLayoutConstraint:0x170c9dba0 h=--&amp; v=--&amp; CustomView:0x1741da7c0.midY == + 25.0&gt;"
  )
  
  Will attempt to recover by breaking constraint 
  &lt;NSLayoutConstraint:0x170c9bfd0 V:[CustomView:0x1741da7c0]-(0)-|   (Names: '|':CustomView:0x1741da8b0 )&gt;
  </code></pre>
  
  <p><strong>Edit #2: It works!</strong></p>
  
  <p>After @Andrea added:</p>
  
  <pre><code>view.translatesAutoresizingMaskIntoConstraints = NO;
  </code></pre>
  
  <p>in his:</p>
  
  <pre><code>- (void) stretchToSuperView:(UIView*) view;
  </code></pre>
  
  <p>method, everything works like I expected.</p>
  
  <p>Thanks to @Andrea.</p>