-->

UINavigationItem titleView “ignored if leftBarButt

2019-01-18 23:24发布

问题:

The documentation for UINavigationItem's titleView property says: "This property is ignored if leftBarButtonItem is not nil."

However, I've set both the titleView and leftBarButtonItem properties in my testing and they both appear to show up fine. I've tested on all the simulators in my Xcode: 4.3, 5.0, and 5.1. Does anyone know if the documentation is just wrong, or is it correct on some older versions (e.g. 4.2, 3.x) that I haven't been able to test?

回答1:

Check the documentation for property leftBarButtonItems, it says:

If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the right side of the bar are not displayed.

So, what's said for titleView is probably incorrect.



回答2:

I think the documentation is wrong, and you should file a bug report.



回答3:

I tested this on iOS6.1 and iOS7. My UINavigationItem had a non-nil titleView and leftBarButtonItem, and both the titleView and leftBarButtonItem were visible. I filed a radar.



回答4:

I've run into this as well. In My specific problem both the titleview and leftbar item would appear. However If I had a Back Button then my titleview would disappear on Push and use the default title. If I have a custom uibuttonitem set for the left it works fine. So It seems to be related to back button. I ended up solving by setting title attributes and no doing a custom view.



回答5:

I just ran into this and found the same thing you did. I also noted that the documentation for UINavigationItem was last updated in 2011, while UINavigationBar got updates in both iOS 5 and 6, so it's possible this behavior changed since the last time the UINavigationItem doc was updated.

Very confusing though.

Noticed this as well in UINavigationController's reference:

"The navigation controller updates the middle of the navigation bar as follows:

...

If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item."

Since there's a leftBarByButton (the back button) by definition anytime you're at the non-root view, it sure seems like a mistake in the UINavigationItem's documentation