NavigationController導航欄中添加多個UIBarButtonItem iPhone軟體開發教學課程

NavigationController導航欄中添加多個UIBarButtonItem 
 
在實際的開發中,導航器是最重要的容器之一,我們經常要在導航欄中添加各種樣式的按鈕,添加一個按鈕很簡單,代碼如下圖: 
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UITabBarSystemItemContacts  
                                                                 target:self action:@selector(clickSettings:)];           
self.navigationItem.rightBarButtonItem = anotherButton;  
[anotherButton release]; 
其中按鈕的樣式可以有多種,具體的可以參考:https://developer.apple.com/library/ios/prerelease/#documentation/UIKit/Reference/UIBarButtonItem_Class/  
在有些項目中要在右面添加兩個按鈕,實現的樣式如下圖: 
  
  
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 45)];  
[tools setTintColor:[self.navigationController.navigationBar tintColor]];  
[tools setAlpha:[self.navigationController.navigationBar alpha]];  
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2]; 
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  
                        target:self action:@selector(clickSettings:)]; 
UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UITabBarSystemItemContacts  
                                                        target:self action:@selector(clickEdit:)];  
[buttons addObject:anotherButton];  
[anotherButton release];  
[buttons addObject:anotherButton1];  
[anotherButton1 release];  
[tools setItems:buttons animated:NO];  
[buttons release];  
UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools];  
self.navigationItem.rightBarButtonItem = myBtn; 
[myBtn release];  
[tools release]; 

 

 

摘自 計算機學習村落

You May Also Like