Adding navigationItem.titleView to the storyboard does not work

I can add a titleView to my navigation element 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 try to do this in my storyboard, and I fail.

Here is what I did:

  • Drag and drop a UIView over my VC navigation element.
  • Drag UIImageView on top of UIView
  • Add width / height / lead / top restrictions
  • Set image property to UIImageView

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

Here's how things look:

enter image description here

I am surprised that it was so difficult. Any ideas?

+7
ios objective-c uistoryboard uinavigationitem uiimageview
source share
4 answers

In the storyboard interface builder

  • Select a view controller
  • In the attribute inspector (⌘⌥ 4) in the "Simulated indicators" section for the "Top panel" selector, select one of the navigation panels
  • You can then drag the UIView over the title on the storyboard.
  • You can change the "Top Bar" setting to "Inferred" if you want

Here's an image of the storyboard after dragging and dropping a UIView over the title:

after dragging UIView over the title

+16
source share

Cannot set auto-detection restrictions for navigation items in a storyboard. I believe that you are actually dragging views onto the actual view controller. You can only drag barButtonItems into navigationItem.

Alternative option -

  • Create a view in a separate xib without using autorun
  • Download xib to code
  • setTranslatesAutoresizingMaskIntoConstraints for true
  • Assign a titleView to a view

From my experience, using autostart with any views in the navigationBar does not work and leads to unexpected results. For example, I ran into errors when the UIActivityViewController caused unexpected constraints to be added to the titleI title.

Before: enter image description here

After presenting the UIActivityViewController and skip step 3: enter image description here

+5
source share

For those who are faced with the problem of adding a custom view or UIButton inside the UIBarButtonItem in Xcode 8, here is the solution: Xcode accepts drag and drop only into the controller's view property now (Xcode 7 is allowed to drag directly to UINavigationItem). If you remove the view controller, this will allow you to drag items to the navigation bar. Therefore, create a new empty navigation controller, delete the main view inside the root view controller, drag and drop elements to the navigation panel, copy this navigation element (not the plan itself) and paste it onto your target controller, it will replace your old navigation elements. After removing this empty controller. PROFIT!

The apple Xcode team seems to be creating new quests for developers with even more passion than their main product.

+2
source share

This strange problem is related to size classes.

In fact, I had the same problem and the solution was:

  • Set the size class for the storyboard Any,Any
  • Drag the view into the navigation bar item.
  • Reset is the size class of your storyboard.

And everything works like a charm!

I think adding a view to the navigation element with a size class not set to Any,Any is causing a problem.

Hope this works for everyone because it really was awful: [.

0
source share

All Articles