This blog post asks the very question I have: How can one visually manage toolbar items in Interface Builder? Sure one can use this technique for the top level view, but what about other views? Is there no way I can give my design partner the ability to edit the toolbar items directly in such a view without the hack of creating a placeholder UIToolbar that's never actually used?
Here's how I did it. I added this property to my controller @interface
:
@property (nonatomic, strong) IBOutletCollection(UIBarButtonItem) NSArray *toolbarItems;
In the @implementation
, I added:
@synthesize toolbarItems;
Then, in the nib, I had all of the UIBarButtonItem
s in a UIToolbar
. In the "Objects" navigator, I selected them all, then moved them out of the UIToolbar, so that they have no view that they are a part of. Then, for each one, I right-clicked, and dragged the circle next to "New Referencing Outlet Collection" to "File's Owner", and selected toolbarItems
in the resulting popup. And now, all of the UIBarButtonItems just magically appear in the UIToolbar, no more work necessary.
Alas, this is not ideal. Now that the UIBarButtonItems
are not in a UIToolbar
in the nib, they are completely invisible interface builder. They are still in the "Objects" navigator, but one cannot actually see what they look like. This seems like an unfortunate bug (filed as rdar://11234043).
The other issue is that there is no way to see the order of the items in the IBOutletCollection
, as far as I can tell. I added them in the order I want them displayed, right-to-left, but it will be a PITA if I ever need to change the order or add new ones somewhere in the middle.
Here's hoping Apple adds nicer IBOutletCollection
support to interface builder, with visible containers of some kind.