title: Trouble positioning custom navigationItem titleView
body: <p>I have a custom view (<code>titleView</code>) that I've created for a webView on my iOS app. The <code>titleView</code> has two labels, a <code>titleLabel</code> and a <code>subTitleLabel</code>. If the <code>titleLabel</code> is too wide for the <code>titleView</code>, I truncate the text and have it fill the whole frame. But the problem happens whenever the <code>titleLabel</code> is smaller that the <code>titleView</code>. I get inconsistent results when I try to calculate the position of the <code>titleLabel</code>'s frame. I just assumed I would take the difference between the width of the <code>titelView.frame.size.width</code> and the <code>titleLabel.frame.size.width</code>and divide that by 2, but it doesn't work. I'm probably missing something stupid, but I just can't see it. One thing to note is that the <code>subTitleLabel</code>'s seem to be positioned fairly well, and though it's not perfect, it's better than the <code>titleLabel</code>'s.</p>
  
  <p>Here are some images (I added borders to the <code>titleView</code> and it's subViews to help show positioning) that show positioning with various length title strings:</p>
  
  <p><a href="https://i.stack.imgur.com/ehxB4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ehxB4.png" alt="offset to the left"></a>
  <a href="https://i.stack.imgur.com/s9Evz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s9Evz.png" alt="slightly offset to the right"></a></p>
  
  <p>Code:</p>
  
  <pre><code>UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width - 88, 34)];
  titleView.clipsToBounds = YES;
  
  UIFont *titleFont = [UIFont systemFontOfSize:16 weight:UIFontWeightThin];
  UIFont *speakerFont = [UIFont systemFontOfSize:10 weight:UIFontWeightLight];
  
  CGSize speakerSize = [[self.speech speakerFullNameAndDate] sizeWithAttributes:@{ NSFontAttributeName : speakerFont }];
  CGSize titleSize = [self.speech.title sizeWithAttributes:@{ NSFontAttributeName : titleFont }];
  
  UILabel *titleLabel = [[UILabel alloc] init];
  UILabel *subTitleLabel = [[UILabel alloc] init];
  
  [titleView addSubview: titleLabel];
  [titleView addSubview:subTitleLabel];
  
  CGFloat titleDifference = (titleView.frame.size.width - titleLabel.frame.size.width) / 2;
  
  titleLabel.text = self.speech.title;
  titleLabel.font = titleFont;
  titleLabel.textAlignment = NSTextAlignmentCenter;
  titleLabel.textColor = [UIColor whiteColor];
  titleLabel.backgroundColor = [UIColor clearColor];
  titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  
  // titleLabel is bigger than the titleView's frame
  if (titleSize.width &gt; titleView.frame.size.width) {
      titleLabel.frame = CGRectMake(0, 0, titleView.frame.size.width - 20, 18);
  } else {
      // titleDifference / 3 seems to be the best number for the frame's x coordinate
      titleLabel.frame = CGRectMake(titleDifference / 3, 0, titleSize.width, 18);
      [titleLabel sizeToFit];
  }
  
  subTitleLabel.text = [self.speech speakerFullNameAndDate];
  subTitleLabel.font = speakerFont;
  subTitleLabel.textAlignment = NSTextAlignmentCenter;
  subTitleLabel.textColor = [UIColor whiteColor];
  subTitleLabel.backgroundColor = [UIColor clearColor];
  // Again, ((titleView.frame.size.width - speakerSize.width) / 3) seems to work best, though it's far from perfect
  subTitleLabel.frame = CGRectMake(((titleView.frame.size.width - speakerSize.width) / 3), 20, speakerSize.width, 12);
  [subTitleLabel sizeToFit];
  
  self.navigationItem.titleView = titleView;
  </code></pre>