2009-10-10 92 views
1

我在我的视图控制器中的代码在视图中没有这样的负载..但UIBarButtonItem不在工具栏的右侧。它在左侧。任何帮助?如何给工具栏的标题也?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]; 

回答

7

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

为了解决这个问题,你必须具有灵活的宽度创建另一个按钮,并补充说,作为工具栏的第一个项目:

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
toolbar.items = [NSArray arrayWithObjects:flexible, playItem, nil]; 
[flexible release]; 
+0

的灵活宽度按钮项目肯定是要走的路, 这里。 – 2009-10-10 10:36:16

相关问题