Navigation bar with multiple buttons

20,150

Solution 1

It's quite easy :)

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

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

Solution 2

Instead of using self.navigationItem.rightBarButtonItem, use

self.navigationItem.rightBarButtonItems //note the plural

This allows you to set an array of buttons rather than a single one.

See the UINavigationItem class reference for details.

Solution 3

  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]
Share:
20,150
Chandler De Angelis
Author by

Chandler De Angelis

I am a Senior iOS Developer at Mammoth Media living in Santa Monica. Making iOS apps is my expertise, and I dabble in front end web development. You can find everything about my career, including my resume, github, linkedin, and portfolio on my personal website.

Updated on July 21, 2022

Comments

  • Chandler De Angelis
    Chandler De Angelis almost 2 years

    I have a navigation bar with a left and right button, and I need to put another button next to the right button. Does anyone know how I can go about this? Here is some code to help:

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