views:

359

answers:

2

I have a UIToolbar that needs three buttons in a radio style, meaning that of the three, only one button can be pushed at a time. The documentation makes reference to the possibility of setting up radio UIBarButtonItems in the class reference definition of the width property:

If this property value is positive, the width of the combined image and title are fixed. If the value is 0.0 or negative, the item sets the width of the combined image and title to fit. This property is ignored if the style uses radio mode. The default value is 0.0.

However, I did a find for "radio" in the UIKit Framework Reference and I can't find any mention of UIBarButtonItems in radio style. I know that I could alternatively use a TabBar for a radio interface, but a TabBar doesn't quite match the purpose of my UI (normal buttons + radio buttons). I see that the Calendar App uses UIBarButtonItems in a radio style (List, Day, Month), so it seems like this should be somewhere in the API and approved by the HIG. Is this hiding somewhere or would I have to create UIBarButtonItems with custom views?

+1  A: 

Have you considered trying a UISegmentedControl? You can set it up so that only one of the segments is "pressed" at a time.

Bryan Kyle
I thought about that but I need this to be in the toolbar and segmented controls can't be resized.
iPhoneToucher
Scratch that, I just read jtbandes's answer.
iPhoneToucher
+5  A: 

A UISegmentedControl is what you want. It's kind of hiding in Interface Builder, as it's a different style outside of a toolbar.

The normal style:

normal segmented control

The same thing in a toolbar:

bar segmented control

You have two options for its behavior: a momentary highlight when tapped, or a radio-style behavior, which is what you want. You can set this with the "Momentary" checkbox in the Attributes inspector:

segmented control attributes

jtbandes
Oohhhh, I didn't know you could add segmented controls into toolbars. I thought only UIBarButtonItems were allowed. Oops! Thanks!
iPhoneToucher
Yeah I just got it. It was tricky to figure this out...I don't use IB much so I had trouble seeing that this was possible. Programatically, you have to create a segmented control in bar style, add that as a custom view to a UIBarButtonItem, then add that to a UIToolbar.
iPhoneToucher
Yeah, that's correct. I'd suggest learning / getting used to using IB more, as it can save a lot of time and make things much easier.
jtbandes