CrystalBlue
CrystalBlue

Reputation: 1863

Position a bar button item in a toolbar

I have a tool bar at the top and bottom of my application and I need to create buttons to put into the toolbars. The ones designing this application would like space placed between the buttons on the toolbar. Aside from manually coding in a position change for the buttons, is there a better way to accomplish this through Interface Builder?

Upvotes: 6

Views: 13719

Answers (2)

EmptyStack
EmptyStack

Reputation: 51374

You can add a bar button of type UIBarButtonSystemItemFlexibaleSpace in the place where you want the space.

UIBarButtonItem *barButton1 = ...
UIBarButtonItem *barButton2 = ...

UIBarButtonItem *flexibleSpaceBarButton = [[UIBarButtonItem alloc] 
                           initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                target:nil
                                                action:nil];

toolbar.items = [NSArray arrayWithObjects:barButton1, 
                                          flexibleSpaceBarButton, 
                                          barButton2, nil];

Upvotes: 25

Ernő Simonyi
Ernő Simonyi

Reputation: 365

As EmptyStack wrote, you can use the UIBarButtonItemFlexibleSpace to create spaces between buttons. You can either use the code written above or do it in the Interface Builder. In IB you will have to place the toolbar and then you can find toolbar items if I remember correcly around the end of the items list. So you can add a flexible space item to the toolbar, play around with its width and then add a 'real' bar button to the toolbar. You can declare outlets and assign actions to the barbuttons just like for normal UIButtons.

Upvotes: 3

Related Questions