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:
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:
I'm surprised this has been so difficult. Any ideas?
In the storyboard interface builder
Here is an image of the storyboard after dragging a UIView over the title:
It is impossible to have autolayout constraints for navigation items in a storyboard. I believe you are actually dragging the views onto the actual view controller. You can only drag in barButtonItems into the navigationItem.
An alternative option is to
From my experience, using autolayout with any views in the navigationBar does not work well and leads to unexpected results. For example, I ran into errors where presenting a UIActivityViewController caused unexpected constraints to be added to the navigationItem's titleView.
Before:
After presenting UIActivityViewController and skipping step 3:
For those who found yourself fighting with problem of adding custom view or UIButton inside UIBarButtonItem in Xcode 8 here is the solution: Xcode accepts dragging only in controller's view property now (Xcode 7 allowed to drag directly to UINavigationItem). If you delete controller's view
, it will allow you to drag items onto the navigation bar. So create new empty navigation controller, remove main view inside root view controller, drag items onto the navigation bar, copy this navigation item (not the bar itself) and paste it to your target controller, it will replace your old navigation items. After remove that empty controller. PROFIT!
It seems apple Xcode team produce new quests for developers with even more passion than their main product.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With