UIBarbuttonItem不在工具栏的右侧?

时间:2009-10-10 07:31:08

标签: 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];

1 个答案:

答案 0 :(得分:7)

playItem指定为rightBarButtonItem的{​​{1}}没有意义,因为这会将按钮设置为顶部导航栏的按钮,并且您想放置它在工具栏中(在底部)

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

self.navigationItem