UIBarbuttonItem不在工具栏的右侧?

sen*_*thu 1 iphone

我在我的视图控制器视图中有代码像这样加载..但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];
Run Code Online (Sandbox Code Playgroud)

Joo*_*ost 7

分配playItem作为rightBarButtonItemself.navigationItem没有意义,因为这将设置按钮,在顶部导航栏的按钮,你想要把它放在工具栏(底部)

要解决此问题,您必须创建另一个具有灵活宽度的按钮,并将其添加为工具栏的第一项:

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = [NSArray arrayWithObjects:flexible, playItem, nil];
[flexible release];
Run Code Online (Sandbox Code Playgroud)