8

I have a navigationController which has a navigation bar. I would really like to have 3 UIBarButtonItems, one on the left, one in the middle, and one on the right. I am able to get the left and right ones added, but how would I add one in the middle, since when using a navigationController, I can't add an array of items to the navBar items property?

Could I somehow add a UIButton, styled like a UIBarButtonItem in the titleView location?

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

1 Answers1

5

taken from apple api, basically you can create a custom UIView that has a UIButton in it and use this as your titleView (notice the note about a leftBarButtonItem causes the titleView to be ignored and not shown): (edit note: this is a property of UINavigationItem)

titleView A custom view displayed in the center of the navigation bar when this item is the top item.

@property(nonatomic, retain) UIView *titleView Discussion If this property value is nil, the navigation item’s title is displayed in the center of the navigation bar when this item is the top item. If you set this property to a custom title, it is displayed instead of the title. This property is ignored if leftBarButtonItem is not nil.

Custom views can contain buttons. Use the buttonWithType: method in UIButton class to add buttons to your custom view in the style of the navigation bar. Custom title views are centered on the navigation bar and may be resized to fit.

Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
  • 1
    The note in the documentation that `leftBarButtonItem` causes the `titleView` to be ignored is interesting. In my tests on iOS 4.3 and iOS 5.1, I found that this is *false*. I've added a button as the left item and added a segmented control as the title and both happily get displayed. – Adrian Schönig Apr 06 '12 at 02:48
  • 1
    @AdrianSchönig, related to your comment: http://stackoverflow.com/q/9690409/280789 – mon4goos Dec 12 '12 at 23:27