title: Building a titleView programmatically with constraints (or generally constructing a view with constraints)
body: <p>I'm trying to build a titleView with constraints that looks like this:</p>
  <p><img src="https://i.stack.imgur.com/7RRKf.png" alt="titleView" /></p>
  <p>I know how I would do this with frames. I would calculate the width of the text, the width of the image, create a view with that width/height to contain both, then add both as subviews at the proper locations with frames.</p>
  <p>I'm trying to understand how one might do this with constraints. My thought was that intrinsic content size would help me out here, but I'm flailing around wildly trying to get this to work.</p>
  <pre><code>UILabel *categoryNameLabel = [[UILabel alloc] init];
  categoryNameLabel.text = categoryName; // a variable from elsewhere that has a category like &quot;Popular&quot;
  categoryNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
  [categoryNameLabel sizeToFit]; // hoping to set it to the instrinsic size of the text?
  
  UIView *titleView = [[UIView alloc] init]; // no frame here right?
  [titleView addSubview:categoryNameLabel];
  NSArray *constraints;
  if (categoryImage) {
      UIImageView *categoryImageView = [[UIImageView alloc] initWithImage:categoryImage];
      [titleView addSubview:categoryImageView];
      categoryImageView.translatesAutoresizingMaskIntoConstraints = NO;
      constraints = [NSLayoutConstraint constraintsWithVisualFormat:@&quot;|[categoryImageView]-[categoryNameLabel]|&quot; options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView, categoryNameLabel)];
  } else {
      constraints = [NSLayoutConstraint constraintsWithVisualFormat:@&quot;|[categoryNameLabel]|&quot; options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
  }
  [titleView addConstraints:constraints];
  
  
  // here I set the titleView to the navigationItem.titleView
  </code></pre>
  <p>I shouldn't have to hardcode the size of the titleView. It should be able to be determined via the size of its contents, but...</p>
  <ol>
  <li>The titleView is determining it's size is 0 unless I hardcode a frame.</li>
  <li>If I set <code>translatesAutoresizingMaskIntoConstraints = NO</code> the app crashes with this error: <code>'Auto Layout still required after executing -layoutSubviews. UINavigationBar's implementation of -layoutSubviews needs to call super.'</code></li>
  </ol>
  <h1>Update</h1>
  <p>I got it to work with this code, but I'm still having to set the frame on the titleView:</p>
  <pre><code>UILabel *categoryNameLabel = [[UILabel alloc] init];
  categoryNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
  categoryNameLabel.text = categoryName;
  categoryNameLabel.opaque = NO;
  categoryNameLabel.backgroundColor = [UIColor clearColor];
  
  UIView *titleView = [[UIView alloc] init];
  [titleView addSubview:categoryNameLabel];
  NSArray *constraints;
  if (categoryImage) {
      UIImageView *categoryImageView = [[UIImageView alloc] initWithImage:categoryImage];
      [titleView addSubview:categoryImageView];
      categoryImageView.translatesAutoresizingMaskIntoConstraints = NO;
      constraints = [NSLayoutConstraint constraintsWithVisualFormat:@&quot;|[categoryImageView]-7-[categoryNameLabel]|&quot; options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView, categoryNameLabel)];
      [titleView addConstraints:constraints];
      constraints = [NSLayoutConstraint constraintsWithVisualFormat:@&quot;V:|[categoryImageView]|&quot; options:0 metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView)];
      [titleView addConstraints:constraints];
      
      titleView.frame = CGRectMake(0, 0, categoryImageView.frame.size.width + 7 + categoryNameLabel.intrinsicContentSize.width, categoryImageView.frame.size.height);
  } else {
      constraints = [NSLayoutConstraint constraintsWithVisualFormat:@&quot;|[categoryNameLabel]|&quot; options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
      [titleView addConstraints:constraints];
      constraints = [NSLayoutConstraint constraintsWithVisualFormat:@&quot;V:|[categoryNameLabel]|&quot; options:0 metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
      [titleView addConstraints:constraints];
      titleView.frame = CGRectMake(0, 0, categoryNameLabel.intrinsicContentSize.width, categoryNameLabel.intrinsicContentSize.height);
  }
  return titleView;
  </code></pre>