Cómo agregar múltiples UIBarButtonItems en el lado derecho de la barra de navegación?


Me gustaría tener más de un solo UIBarButtonItem en el lado derecho de mi UINavigationBar. ¿Cómo puedo lograr esto?

Un ejemplo de lo que estoy intentando se muestra a continuación: puede notar que la parte superior derecha tiene más de un botón.

introduzca la descripción de la imagen aquí

Author: djromero, 2015-05-20

6 answers

Use esto en swift:

override func viewDidLoad() {
  super.viewDidLoad()

  let editImage    = UIImage(named: "plus")!
  let searchImage  = UIImage(named: "search")!

  let editButton   = UIBarButtonItem(image: editImage,  style: .Plain, target: self, action: "didTapEditButton:")
  let searchButton = UIBarButtonItem(image: searchImage,  style: .Plain, target: self, action: "didTapSearchButton:")

  navigationItem.rightBarButtonItems = [editButton, searchButton]
}

Escriba las funciones de acción de esta manera:

func didTapEditButton(sender: AnyObject){
    ...
}

func didTapSearchButton(sender: AnyObject){
    ...
}
 83
Author: Dharmbir Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-11 09:10:23
-(void)viewDidLoad{

    UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Button_1" style:UIBarButtonItemStylePlain target:self action:@selector(button_1:)];

    UIBarButtonItem *anotherButton2 = [[UIBarButtonItem alloc] initWithTitle:@"Button_" style:UIBarButtonItemStylePlain target:self action:@selector(button_2:)];

    self.navigationItem.rightBarButtonItems=@[anotherButton1,anotherButton2];
}
 3
Author: Anoop,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-20 06:54:40

Creo que nada de lo anterior va a funcionar

Prueba esto

var burgerItem = UIBarButtonItem(image: UIImage(named:"categories"), style: .Plain, target: self, action: "categories")
        var weatherItem = UIBarButtonItem(title: "Weather", style: .Plain, target: self, action: "weather")


        burgerItem.tintColor = UIColor.whiteColor()
        weatherItem.tintColor = UIColor.whiteColor()

        navigationItem.setRightBarButtonItems([burgerItem,weatherItem], animated: true)

Tienes que usar navigationItem.setRightBarButtonItems y ser cuidadoso. navigationItem tiene que ser de un controlador de vista.

class testViewController:UIViewController {

ovverride func viewDidLoad() {
    self.navigationItem.setRightBarButtonItems(...

}

}
 3
Author: Christos Hadjikyriacou,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-20 09:08:50

Esto puede ser de ayuda para usted en objective-c ,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"Title";
        UIBarButtonItem *logOutButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(ButtonClickedAtIndex1:)];
        [logOutButton setTintColor:[UIColor blackColor]];
        logOutButton.tag = 1;

        UIBarButtonItem *syncBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Sync" style:UIBarButtonItemStylePlain target:self action:@selector(ButtonClickedAtIndex1:)];
        [syncBarButtonItem setTintColor:[UIColor blackColor]];
        syncBarButtonItem.tag = 2;

        self.navigationItem.leftBarButtonItem = logOutButton;
        self.navigationItem.rightBarButtonItem = syncBarButtonItem;

       float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

       if (systemVersion >= 7.0) {
           self.edgesForExtendedLayout = UIRectEdgeNone;
           self.navigationController.navigationBar.translucent = NO;
       }
    }
    return self;
}
 2
Author: B.Saravana Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-17 12:27:05

En Swift 3 puedes usar:

let editImage   = UIImage(named: "plus")!
let searchImage = UIImage(named: "search")!

let editButton   = UIBarButtonItem(image: editImage,  style: .plain, target: self, action: #selector(didTapEditButton))
let searchButton = UIBarButtonItem(image: searchImage,  style: .plain, target: self, action: #selector(didTapSearchButton))

navigationItem.rightBarButtonItems = [editButton, searchButton]
 2
Author: oscar castellon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-19 20:26:18

Simplemente agregue este código:

self.navigationItem.leftBarButtonItem = nil

let button = UIButton(type: .custom)
button.setImage(UIImage (named: "ChatTab"), for: .normal)
button.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)

let button2 = UIButton(type: .custom)
button2.setImage(UIImage (named: "ActivityTab"), for: .normal)
button2.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)

let barButtonItem2 = UIBarButtonItem(customView: button2)
self.navigationItem.rightBarButtonItems = [barButtonItem, barButtonItem2]

Este es el resultado:

introduzca la descripción de la imagen aquí

 1
Author: Mr.Javed Multani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-07 07:53:39