14

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

SmartTree
  • 1,381
  • 3
  • 21
  • 40

2 Answers2

36

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

IronManGill
  • 7,222
  • 2
  • 31
  • 52
  • Also check out these links http://stackoverflow.com/questions/602717/aligning-uitoolbar-items and http://stackoverflow.com/questions/6021138/how-to-adjust-uitoolbar-left-and-right-padding – IronManGill Oct 13 '12 at 07:02
6

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")
lifeisfoo
  • 15,478
  • 6
  • 74
  • 115