UIBarbuttonItem is not in right side of tool bar?
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];
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];
精彩评论