Adding navigationItem.titleView in storyboard is failing

djibouti33 picture djibouti33 · Apr 11, 2015 · Viewed 7.1k times · Source

I can add a titleView to my navigationItem very easily in code:

UIImageView *navImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav_logo"]];
UIView *titleView = [[UIView alloc] initWithFrame:navImageView.frame];
[titleView addSubview:navImageView];
self.navigationItem.titleView = titleView;

However, I'm trying to do this within my storyboard, and am failing miserably.

Here's what I've done:

  1. Drag and drop a UIView on top of my VC's Navigation Item.
  2. Drag and drop a UIImageView on top of the UIView
  3. Add width/height/leading/top constraints
  4. Set the image property of the UIImageView

I also set the background of my UIView to red (well ok, more like a burnt orange) to illustrate the problem.

Here's how things render out:

enter image description here

I'm surprised this has been so difficult. Any ideas?

Answer

mattliu picture mattliu · May 7, 2015

In the storyboard interface builder

  • Select your view controller
  • In the the attribute inspector (⌘⌥ 4), under "Simulated Metrics", for the "Top Bar" selector, choose one of the Navigation Bars
  • Then you should be able to drag a UIView over the title on the Storyboard
  • You can change the "Top Bar" setting back to "Inferred" if you'd like

Here is an image of the storyboard after dragging a UIView over the title:

after dragging UIView over the title