2012-08-22 44 views
1

對於UINavigationItem,方法setRightBarButtonItems:animated:在iOS4中不受支持。setRightBarButtonItems:ios4不支持動畫

我怎樣才能重寫這段代碼在欄中右邊添加2個按鈕?

[viewController.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:helpButton, settingsButton, nil] animated:YES]; 

感謝

回答

0

您可以設置按鈕的alpha爲0,你將它添加到視圖之前,然後動畫它的價值爲1.0。

//..Create the buttons 
//Make it invisible 
helpButton.alpha = 0.0; 
settingsButton.alpha = 0.0 

//Add them to the navBar 
[self.navigationController.navigationBar addSubview:helpButton]; 
[self.navigationController.navigationBar addSubview:settingsButton]; 

//Animate it to 1.0 
[UIView animateWithDuration:0.3 animations:^(void) { 
     helpButton.alpha = 1.0; 
     settingsButton.alpha = 1.0 
    }]; 
+0

那麼,我應該使用addSubview,什麼是最好的方式來顯示它們在右邊? – aneuryzm

+0

只需設置合適的框架 –

+0

所以我確實需要計算框架的值,謝謝 – aneuryzm

0

如果要在iOS4中顯示多個按鈕,則必須手動創建它們。

例如,你可以設置titleview的一個自定義視圖並做你想做的存在通過重寫視圖控制器的的setTitle:

- (void)setTitle:(NSString *)title 
{ 
     UIView *titleView = (UIView *)self.navigationItem.titleView; 
     if(!titleView) 
     { 
      titleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40.0)]; 
      titleView.backgroundColor = [UIColor greenColor]; 
      self.navigationItem.titleView = titleView; 
     } 
} 

而且你需要使用UIButton代替UIBarButtonItem的。

+0

不應該有[titleView發佈];在self.navigationItem.titleView = titleView;因爲你已經分配並初始化了titleView 3行嗎? –

+1

我很習慣ARC,我幾乎忘了保留/釋放存在:)如果你不使用ARC,你必須插入一個當然的版本。 – lars

+0

你沒有談論按鈕。你談到標題 – KarenAnne