tags:

views:

377

answers:

1

i have code in my view controller in view did load like this..but the UIBarButtonItem is not in right side of tool bar .it is in left side .any help?how to give title to toolbar also?

UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;

// create a bordered style button with custom title
UIBarButtonItem *playItem = [[[UIBarButtonItem alloc] initWithTitle:@"Play" 
            style:UIBarButtonItemStyleBordered  
            target:self
            action:@selector(flipToSchedules:)] autorelease];
self.navigationItem.rightBarButtonItem = playItem;
NSArray *items = [NSArray arrayWithObjects: playItem,  nil];
toolbar.items = items;

// size up the toolbar and set its frame
// please not that it will work only for views without Navigation toolbars. 
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGRect mainViewBounds = self.view.bounds;

[toolbar setFrame:CGRectMake(0, 20, 
             CGRectGetWidth(mainViewBounds), toolbarHeight)];
[self.view addSubview:toolbar];
+2  A: 

Assigning the playItem as the rightBarButtonItem of self.navigationItem does not make sense, since that will set the button as a button of the navigation bar at the top, and you want to place it in the toolbar (at the bottom)

To solve this, you'll have to create another button with a flexible width and add that as the first item of the toolbar:

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = [NSArray arrayWithObjects:flexible, playItem, nil];
[flexible release];
JoostK
A flexible-width button item is definitely the way to go, here.
Alex Reynolds