24

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?

Community
  • 1
  • 1
user623396
  • 1,537
  • 1
  • 17
  • 39

7 Answers7

9

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

JerryZhou
  • 4,566
  • 3
  • 37
  • 60
2

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]];
2

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

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Gulfam Khan
  • 1,010
  • 12
  • 23
0

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!

nemesis
  • 1,349
  • 1
  • 15
  • 30
0

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

Bhaumik Desai
  • 213
  • 1
  • 9
0

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

0

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)

}
lifeisfoo
  • 15,478
  • 6
  • 74
  • 115