Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple UIBarButtonItems on right side of Navigation Bar?

I would like to have more than a single UIBarButtonItem on the right side of my UINavigationBar. How can I achieve this?

An example of what I am trying are shown below - you can notice that the top right has more than one button.

enter image description here

like image 288
user1406716 Avatar asked May 20 '15 05:05

user1406716


4 Answers

Use this in 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]
}

Write the action functions like this:

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

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

Swift 4 & 5

    override func viewDidLoad() {
        super.viewDidLoad()
        let editImage    = UIImage(named: "edit")!
        let searchImage  = UIImage(named: "search")!

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

        navigationItem.rightBarButtonItems = [editButton, searchButton]
    }

    @objc func didTapEditButton(sender: AnyObject){

    }

    @objc func didTapSearchButton(sender: AnyObject){

    }
like image 153
Dharmbir Singh Avatar answered Nov 01 '22 13:11

Dharmbir Singh


-(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];
}
like image 22
Anoop Avatar answered Nov 01 '22 14:11

Anoop


In Swift 3 you can use:

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]
like image 6
oscar castellon Avatar answered Nov 01 '22 13:11

oscar castellon


I think nothing of the above is going to work

Try this

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)

You have to use navigationItem.setRightBarButtonItems and be carefull. navigationItem has to be of a view controller.

class testViewController:UIViewController {

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

}

}
like image 5
Christos Chadjikyriacou Avatar answered Nov 01 '22 12:11

Christos Chadjikyriacou