2013-01-15 121 views
20

我有一個帶有左右按鈕的導航欄,我需要在右側按鈕旁邊放置另一個按鈕。有誰知道我可以怎麼做呢?下面是一些代碼,以幫助:帶有多個按鈕的導航欄

- (id)init { 
    self = [super initWithStyle:UITableViewStyleGrouped]; 
    if (self) { 

     _pinArray = [[NSArray alloc]init]; 
     _pinArray = [Data singleton].annotations; 

     UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"Map" 
                   style:UIBarButtonItemStylePlain 
                  target:self 
                  action:@selector(goToMap:)]; 
     self.navigationItem.rightBarButtonItem = right; 

     UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"Menu" 
                  style:UIBarButtonItemStylePlain 
                  target:self 
                  action:@selector(goToMenu:)]; 
     self.navigationItem.leftBarButtonItem = left; 
     self.navigationItem.title = @"My Homes"; 
    } 
    return self; 
} 

回答

7

而不是使用self.navigationItem.rightBarButtonItem的,使用

self.navigationItem.rightBarButtonItems //note the plural 

這允許你設置一個按鈕陣列而不是單個按鈕。

有關詳細信息,請參閱UINavigationItem class reference

0
let RightBarButton = UIButton() 
     RightBarButton.setTitleColor(UIColor.blueColor(), forState: .Normal) 
     RightBarButton.frame = CGRectMake(30,0,30,30) 
     RightBarButton.setImage(UIImage(named: "search-icon.png"), forState: .Normal) 
     RightBarButton.addTarget(self, action: #selector(BaseViewController.OpenQuickLink), forControlEvents: .TouchUpInside) 

     let RightBarButton2 = UIButton() 
     RightBarButton2.setTitleColor(UIColor.blueColor(), forState: .Normal) 
     RightBarButton2.frame = CGRectMake(0,0,30,30) 
     RightBarButton2.setImage(UIImage(named: "share-icon.png"), forState: .Normal) 
     RightBarButton2.addTarget(self, action: #selector(BaseViewController.Opensharelink), forControlEvents: .TouchUpInside) 
     let barButtonItem1 = UIBarButtonItem(customView: RightBarButton2) 

     let barButtonItem = UIBarButtonItem(customView: RightBarButton) 


navigationItem.rightBarButtonItems = [barButtonItem1, barButtonItem2]