我的视图控制器中有这样的代码。但是
viewDidLoad
不在工具栏的右侧。它位于左侧。如何给工具栏加上标题?UIBarButtonItem
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];
指定为
playItem
的 rightBarButtonItem
没有意义,因为这会将按钮设置为顶部导航栏的按钮,并且您希望将其放置在工具栏(底部)中)要解决此问题,您必须创建另一个具有灵活宽度的按钮并将其添加为工具栏的第一项:
self.navigationItem