views:

1106

answers:

2

I have created a flexible navigation bar in my app that will show custom buttons on the left, right, and in the middle. So far I have got working:

  • Right/Left/Middle - Custom Image and/or Text
  • Right/Left - Normal looking button with custom Image in it
  • Right/Left - Normal looking button with custom Text in it

By 'normal looking' I mean the default UIBarButtonItemStyle- just a nice shiny button.

My question is, how can I achieve the same look in the MIDDLE of the nav bar? I can do custom/text images by just creating a custom UIButton, and on the right / left I create a UIBarButtonItem from it, but in the middle I just add the UIButton view as a subview of the navbar.

I can't add a UIBarButtonItem to the navbar as a subview, and any UIButton I create doesn't look like a UIBarButtonItem.

The only workaround I can think of is to use a stretchable custom image that I steal from a screenshot. I'd rather avoid doing it this way. Am I missing something?

******* UPDATE ********** The open source library Three20 will allow you to create UIButtons that look like UIBarButtonItems. Then you can add them to the bar view either by placing manually as a subview or by setting the title view.

A: 

Put a flexible region on the left and right, they will offset each other causing anything between them to be centered

slf
+1  A: 

I never understood why they didn't make UIBarButtonItems derive from UIViews (or even better, UIButtons) so they could be used elsewhere.

Sounds like you're on the right track, but need better artwork :-) You can get a large number of iPhone UI components in a Photoshop .PSD file from here. They have pre-rendered bar buttons which you can use as a base for a standard UIButton image. If you have Photoshop (or a decent drawing program) you can stretch the buttons from the middle to fit your size without getting the corners distorted. Just add the label and you should be good to go.

Ramin
Thanks. By the way, you can stretch the image from the middle programmatically (search stretchableImage). I also think it is very odd that a UIBarButtonItem doesn't inherit from UIButton.
Sam