Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UIBarButtonItem to navigationItem.titleView?

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?

like image 927
Nic Hubbard Avatar asked Jan 31 '11 22:01

Nic Hubbard


1 Answers

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.

like image 174
Jesse Naugher Avatar answered Sep 20 '22 00:09

Jesse Naugher