2009-10-10 96 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

相關問題