user623396
user623396

Reputation: 1547

Add multiple buttons to navigation bar in storyboard

I wonder if this can be achieved using Storyboard IB:

self.navigationItem.leftBarButtonItems = @[barButton1, barButton2]

EDITED:

The purpose is to preserve the benefits of using storyboard and if it's possible without using any lines of code.

EDIT #2:

Based on this answer I experimented and saw that it is possible to link the Title View property of the Navigation Item to a Toolbar enter image description here

but it looks to me like a hack (and I might want to preserve the Title as well):

enter image description here

Any better idea?

Upvotes: 24

Views: 18232

Answers (7)

Gulfam Khan
Gulfam Khan

Reputation: 1089

1 -> Drag and drop Bar Button Item in navigation bar and in latest xcode you can direclty place more than one items in navigation bar either on left or right side

see this for example

Upvotes: 2

Niranjan kumar
Niranjan kumar

Reputation: 21

Can use below code :

UIBarButtonItem *btnShare = [[UIBarButtonItem alloc] initWithCustomView:self.backbtn];
UIBarButtonItem *btnRefresh = [[UIBarButtonItem alloc] initWithCustomView:self.testbutton2];   
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:btnShare,btnRefresh, nil]];

Upvotes: 2

lifeisfoo
lifeisfoo

Reputation: 16294

If you want to add them from the code, in Swift you can do:

override func viewDidLoad() {
    // ....
    let ubRefresh = UIBarButtonItem.init(barButtonSystemItem: .Refresh, target: self, action: "refreshManually")
    let ubMyIcon = UIBarButtonItem(image: UIImage(named: "MyIcon"), style: .Plain, target: self, action: "myAction")
    self.navigationItem.setRightBarButtonItems([ubMyIcon, ubRefresh], animated: true)

}

Upvotes: 0

JerryZhou
JerryZhou

Reputation: 5166

There a way to add a view to navigationItem. Than add buttons or labels you need to the view.

enter image description here

enter image description here

Upvotes: 9

Abhay Singh Naurang
Abhay Singh Naurang

Reputation: 141

I would suggest you to add multiple button on toolbar . Adding multiple button on navigation bar is not a good idea . if you want add multiple button on toolbar using storyboard . this video link might help you to make a scrollable toolbar with multiple button https://www.youtube.com/watch?v=4nYM8EfTpRw

Upvotes: 0

Bhaumik Desai
Bhaumik Desai

Reputation: 213

You can create Your buttons in storyboard edit/style them as you like then create their outlets and add them as top level objects and then add them at run time at viewDidLoad using [navigationItem setLeftBarButtonItems:] and [navigationItem setRightBarButtonItems:... Hope that Helps :)

Upvotes: 0

nemesis
nemesis

Reputation: 1349

You could create a UIToolbar containing all yours buttons:

UIToolbar *toolbar = [[UIToolbar alloc] init];
// ... set toolbars' frame, initialize buttons, etc;
[toolbar setItems:@[barButton1, barButton2] animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];

Or you could use setLeftBarButtonItems:animated: and setRightBarButtonItems:animated:

Hope that helps, good luck!

Upvotes: 0

Related Questions