80

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

djromero
  • 19,551
  • 4
  • 71
  • 68
user1406716
  • 9,565
  • 22
  • 96
  • 151
  • possible duplicate of [How to add 2 buttons into the UINavigationbar on the right side without IB?](http://stackoverflow.com/questions/1803609/how-to-add-2-buttons-into-the-uinavigationbar-on-the-right-side-without-ib) – Rahul Mane May 20 '15 at 05:57

11 Answers11

180

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){

    }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
12
-(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];
}
CW0007007
  • 5,681
  • 4
  • 26
  • 31
Anoop
  • 409
  • 3
  • 13
6

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]
oscar castellon
  • 3,048
  • 30
  • 19
6

Simply add this code:

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]

This is the result:

enter image description here

Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52
5

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(...

}

}
3

With Swift 4

let editImage       = UIImage(named: "toolbar_edit")!
let favoriteImage   = UIImage(named: "toolbar_fav_solid")!

let editButton      = UIBarButtonItem(image: editImage,  style: .plain, target: self, action: #selector(didTapEditButton))
let favoriteButton  = UIBarButtonItem(image: favoriteImage,  style: .plain, target: self, action: #selector(didTapFavoriteButton))

navigationItem.rightBarButtonItems = [editButton, favoriteButton]

Click action for those buttons

@objc func didTapEditButton(sender: AnyObject) {
    print("edit")
}

@objc func didTapFavoriteButton(sender: AnyObject) {
    print("favorite")
}

Output screenshot (iPhone X)

enter image description here

Kavin Varnan
  • 1,989
  • 18
  • 23
1

Accepted answer as well as few of the answer is absolutely correct but in my case none of them work.

Reason : My View Controller was Inherited from UIViewController and I had to set Add to Cart Button and Search Button On Right hand side. And All my view controllers were a part of UITabbarController.

    let editImage = UIImage(named: "OrdersTabIcon")
    let searchImage = UIImage(named: "OrdersTabIcon")
    let editButton = UIBarButtonItem(image: editImage, style: .plain, target: self, action: #selector(iconTapped))
    let searchButton = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(iconTapped))

Instead of using

 self.navigationItem.setRightBarButtonItems([editButton, searchButton], animated: true)

Use This .

self.navigationController?.navigationBar.topItem?.setRightBarButtonItems([editButton, searchButton], animated: true)
Shanu Singh
  • 365
  • 3
  • 16
0

This is may be help for you in ,

- (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;
}
djromero
  • 19,551
  • 4
  • 71
  • 68
B.Saravana Kumar
  • 1,208
  • 6
  • 20
  • 48
0

Use navigationItem's rightBarButtonItems, which takes an array of UIBarButton. Appledoc

let share = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItems = [add, share]
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
ibyte
  • 463
  • 4
  • 17
  • 1
    Please put your answer always in context instead of just pasting code. See [here](https://stackoverflow.com/help/how-to-answer) for more details. – gehbiszumeis Jan 28 '19 at 12:21
  • Code-only answers are discouraged. Please click on [edit] and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. [From Review](https://stackoverflow.com/review/low-quality-posts/22049261) – Nick Jan 29 '19 at 02:02
0

I don't know wether an Interface Builder solution was not possible before, but at least with the most recent update (Xcode 11.0), it is possible to drag two UI Bar Buttons into the rightBarButton space. Two outlets and actions can then be dragged into the code like always.

Since the OP didn't ask for a solution without IB, I think this also qualifies as an answer.

bjrne
  • 355
  • 5
  • 14
-1

For Xamarin iOS with the new updates, it is like:

UIBarButtonItem Button1 = new UIBarButtonItem(UIImage.FromBundle("Image1"), UIBarButtonItemStyle.Plain,YourEventHandler);
UIBarButtonItem Button2 = new UIBarButtonItem(UIImage.FromBundle("Image1"), UIBarButtonItemStyle.Plain,YourEventHandler);

NavigationItem.SetRightBarButtonItems(new[]{ Button1, Button2 }, true);
TylerP
  • 9,600
  • 4
  • 39
  • 43
Chandu Ranwala
  • 305
  • 1
  • 4
  • 18