Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 620
CrystalBlue Avatar asked Jul 18 '11 14:07

CrystalBlue


People also ask

How do I place a button on the toolbar?

Step 1: Place a button on the toolbar in the spot where you want the control (s) to eventually be. YOU MUST place a seperator on either side of the button!. Give the button an easily remembered resource name such as IDP_PLACEHOLDER2 in the example below.

What is a toolbar?

Thank you. A toolbar is a control that contains one or more buttons. Each button, when clicked by a user, sends a command message to the parent window. Typically, the buttons in a toolbar correspond to items in the application's menu, providing an additional and more direct way for the user to access an application's commands.

How do I add bar items to the main menu?

The main menu will contain three sub-menus (File, Edit and View), each having its own bar items. The second bar will display the Output button, which is also available via the View submenu of the first bar: Bars are added to the BarManager.Bars collection. Bar items are added to bars via the Bar.AddItem and Bar.AddItems methods.

What is the state of a button in a toolbar?

Each button in a toolbar has a state. The toolbar updates a button's state to reflect user actions, such as clicking the button. The state indicates whether the button is currently pressed or not pressed, enabled or disabled, hidden or visible.


2 Answers

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];
like image 175
EmptyStack Avatar answered Sep 27 '22 23:09

EmptyStack


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.

like image 34
Ernő Simonyi Avatar answered Sep 27 '22 23:09

Ernő Simonyi