Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a button on the right side of the toolbar?

I created a toolbar programmatically:

UIToolbar *boolbar = [UIToolbar new];
    boolbar.barStyle = UIBarStyleDefault;
    boolbar.tintColor = [UIColor orangeColor];
    [boolbar sizeToFit];

And then added a button to it:

UIBarButtonItem *cancelleftBarButton =[[UIBarButtonItem alloc]initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(tapBackGround:)];

cancelleftBarButton.tintColor = [UIColor orangeColor];

NSArray *array = [NSArray arrayWithObjects:cancelleftBarButton, nil];
[boolbar setItems:array animated:YES];

However, this button appears only at the left side of the toolbar. Is it possible to put it on the right side of the toolbar ?

enter image description here

like image 495
SmartTree Avatar asked Oct 13 '12 06:10

SmartTree


People also ask

Where is toolbar button?

The toolbar, also called bar or standard toolbar, is a row of buttons, often near the top of an application window, that controls software functions. The boxes are below the menu bar and often contain images corresponding with the function they control, as demonstrated in the image below.

Which control is used to add a toolbar at the top of your form?

The ToolBar control allows you to create toolbars by adding Button objects to a Buttons collection. You can use the Collection Editor to add buttons to a ToolBar control; each Button object should have text or an image assigned, although you can assign both.


2 Answers

Here is the method to add the UIBarButtonItem on the right side of the toolbar.

UIBarButtonItem *leftButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem1Pressed:)] autorelease];

UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];

UIBarButtonItem *rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem2Pressed:)] autorelease];

OR

If you are attempting to do it from the XIB , then .

Insert an item which has identifier being "flexible space".

enter image description here

like image 152
IronManGill Avatar answered Sep 17 '22 20:09

IronManGill


In Swift

let btn1 = UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Done, target: self, action: "btn1Pressed"
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let btn2 = UIBarButtonItem(title: "Button 2", style: UIBarButtonItemStyle.Done, target: self, action: "btn2Pressed")
like image 32
lifeisfoo Avatar answered Sep 18 '22 20:09

lifeisfoo