Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place UIBarButtonItem on the right side of a UIToolbar?

I have designed a view with a toolbar that appears modally in Interface Builder. I have a UIBarButtonItem that is on the left hand side, which I would like to appear on the right hand side of the toolbar. How can I do this in IB, or via code?

like image 859
Sheehan Alam Avatar asked Mar 22 '10 15:03

Sheehan Alam


3 Answers

Here's how to do it in code if anyone comes across this post:

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];


self.toolbarItems = [NSArray arrayWithObjects: leftButton, flex, rightButton, nil];
like image 149
Paul D Avatar answered Nov 09 '22 04:11

Paul D


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


(source: xanga.com)

like image 40
kennytm Avatar answered Nov 09 '22 03:11

kennytm


- one item in the right like this

enter image description here

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1” style:UIBarButtonItemStylePlain target:self action:nil];

self.toolbarItems = [NSArray arrayWithObjects: flexible, item1, nil];

-two items left and right like this

enter image description here

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1” style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2” style:UIBarButtonItemStylePlain target:self action:nil];

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, nil];

-three items like this

enter image description here

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1” style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2” style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"item3” style:UIBarButtonItemStylePlain target:self action:nil];

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, flexible, item3, nil];

-four items like this

enter image description here

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1” style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2” style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"item3” style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithTitle:@"item4” style:UIBarButtonItemStylePlain target:self action:nil];

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, item3, flexible, item4, nil];

so when you want to add a space you must add the flexible bar button.

like image 35
Mohamad Chami Avatar answered Nov 09 '22 04:11

Mohamad Chami