title: UIView created from nib and autolayout not constrained to superview
body: <p>In my last ios project I've been using a subview with these components and constraints:</p>
  
  <pre><code>V:|-5-[image(70)]-5-[label]-5-[button]-5-|
  H:|-5-[label]-5-|
  </code></pre>
  
  <p>Everything works well if this subview is created in a <code>ViewController</code> with IB. 
  Now I need to create this subview in some others <code>ViewControllers</code>, so I've thought that is a good moment to create a reusable custom subview from a nib instead of create it in each view controller. 
  I've used these same components and constraints and I've set <code>translatesAutoresizingMaskIntoConstraints</code> to <code>NO</code>.</p>
  
  <p>When I want to use this custom view this is what I'm doing in my superview:</p>
  
  <pre><code>PopupView *customView = [[PopupView alloc] initWithFrame:CGRectZero];
  [customView setMessage:@"This is supposed to a be a large text. This is supposed to a be a large text"];
  [self.view addSubview:customView];
  
  NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(customView);
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|" 
                              options:0 
                              metrics:nil 
                            views:viewsDictionary]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" 
                              options:0 
                              metrics:nil 
                            views:viewsDictionary]];
  </code></pre>
  
  <p>The problem is: if the text for my label is too large, <code>customView</code> has a very large width (the needed to contain the label) and is not constrained to my superview. I thought that the last lines of my code would do that, but it seems that I'm missing something.</p>
  
  <p>Do I need to do anything else so my <code>customView</code> is limited to my superview bounds?</p>
  
  <p><strong>EDIT 1:</strong>
  I think is better if I put my code, this is a shorter version of my real code but it fails too:</p>
  
  <p>PopupView.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface PopupView : UIView
  {
      UILabel *messageLb;
  }
  
  @property (nonatomic, strong) IBOutlet UILabel *messageLb;
  
  - (void) setMessage:(NSString *)message;
  
  @end
  </code></pre>
  
  <p>PopupView.m</p>
  
  <pre><code>#import "PopupView.h"
  
  @implementation PopupView
  
  @synthesize messageLb = _messageLb;
  
  - (instancetype)initWithCoder:(NSCoder *)aDecoder
  {
      self = [super initWithCoder:aDecoder];
      if (self)
      {
          [self commonInit];
      }
      return self;
  }
  
  - (instancetype)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self)
      {
          [self commonInit];
      }
      return self;
  }
  
  - (void)commonInit
  {
      UIView *view = nil;
      NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil];
      for (id object in objects)
      {
          if ([object isKindOfClass:[UIView class]])
          {
              view = object;
              break;
          }
      }
  
      if (view != nil)
      {
          view.translatesAutoresizingMaskIntoConstraints = NO;
          [self addSubview:view];
      }
  }
  
  - (void) setMessage:(NSString *)message
  {
      [_messageLb setText:message];
  }
  
  @end
  </code></pre>
  
  <p>PopupView.xib</p>
  
  <pre><code>View
   |___ View (with constraints 0-0-0-0 to its superview)
          |___ label (with numberoflines 0 and constraints 5-5-5-5 to its superview)
  </code></pre>
  
  <p><strong>EDIT 2:</strong> I've seen that I have some autolayout warnings I didn't see last night (I was too sleepy sorry).</p>
  
  <pre><code>Unable to simultaneously satisfy constraints.
  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:0x145982e0 'UIView-Encapsulated-Layout-Height' H:[UIView:0x14593210(320)]&gt;",
      "&lt;NSAutoresizingMaskLayoutConstraint:0x14592030 h=-&amp;- v=-&amp;- 'UIView-Encapsulated-Layout-Top' H:|-(0)-[UIView:0x14593210]   (Names: '|':UITransitionView:0x14599db0 )&gt;",
      "&lt;NSLayoutConstraint:0x145bdc40 H:|-(0)-[PopupView:0x14592b60]   (Names: '|':UIView:0x14593210 )&gt;",
      "&lt;NSLayoutConstraint:0x145adeb0 H:[PopupView:0x14592b60]-(0)-|   (Names: '|':UIView:0x14593210 )&gt;",
      "&lt;NSAutoresizingMaskLayoutConstraint:0x145a32c0 h=--&amp; v=--&amp; PopupView:0x14592b60.midX ==&gt;"
  )
  Will attempt to recover by breaking constraint 
  &lt;NSLayoutConstraint:0x145adeb0 H:[PopupView:0x14592b60]-(0)-|   (Names: '|':UIView:0x14593210 )&gt;
  </code></pre>
  
  <p>I have these warnings with Vertical too.
  I can see that due to this conflicts the rule that pin my view to the right margin is broken.
  I've tried 2 things with no success:</p>
  
  <ul>
  <li>I set self.view.translatesAutoresizingMaskIntoConstraints = NO; in viewWillAppear method of the ViewController that uses this popup. >> Still the same warnings.</li>
  <li><p>I've changes the priority of my popup constraints. For example, this:</p>
  
  <pre><code>NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(customView);
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
      @"H:|-0@1-[customView]-0@1-|" options:0 metrics:nil views:viewsDictionary]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
      @"V:|-0@1-[customView]-0@1-|" options:0 metrics:nil views:viewsDictionary]];
  </code></pre></li>
  </ul>
  
  <p>This removes all the vertical and horizontal warnings, but my popup is very large yet.</p>