2013-01-15 17 views
20

Mam pasek nawigacji z lewym i prawym przyciskiem, i muszę umieścić kolejny przycisk obok prawego przycisku. Czy ktoś wie, jak mogę to zrobić? Oto kod, aby pomóc:Pasek nawigacyjny z wieloma przyciskami

- (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; 
} 

Odpowiedz

44

Jest to dość proste :)

https://developer.apple.com/documentation/uikit/uinavigationitem/1624956-rightbarbuttonitems

navigationItem.rightBarButtonItems = [rightA, rightB] // @[rightA, rightB] for ObjC 
+0

Czy istnieje maks. 2? – thebiglebowski11

+0

W rzeczywistości nie ma maks. Możesz dodać tyle, ile chcesz, nawet na szkodę lol. Ale tak naprawdę to tyle, ile możesz zmieścić wygodnie. Pomyśl o swoich użytkownikach! –

+0

Co zrobić, jeśli chcemy wyłączyć którekolwiek z nich? –

7

Zamiast self.navigationItem.rightBarButtonItem użyć

self.navigationItem.rightBarButtonItems //note the plural 

To pozwala ustawić tablica przycisków zamiast pojedynczego.

Aby uzyskać szczegółowe informacje, patrz 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] 
Powiązane problemy