Mandar Apte

UI/UX Designer from Mumbai, Maharashtra, India.

How to add four or more navigation bar buttons in your iOS App XCode Project

I assume that you have added a navigation controller through Editor > Embed In > Navigation Controller in the xib (or formally nib) file in your XCode Projects’ Storyboard

Problem:
For example, you have added an Action button for Tweet Sheet on the Left, a Menu button on the right, and a Title button in the middle. But these three items don’t allow you to add a button at the second left position. This additional functionality enriches your app’s usp (unique selling point) or usability.

Reason:
It turns out this is a limitation of Interface Builder. It will only let you configure a single left and correct bar button item in a navigation bar. Don’t worry; you can still configure your remaining items in code. For example,

Solution:
Add the following code in the ‘viewDidLoad’ function in your View Controller’s Header file, i.e .h file


let email = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: Selector("someAction"))
if let leftBarButtonItems = navigationItem?.leftBarButtonItems {
  navigationItem?.leftBarButtonItems = leftBarButtonItems + [email]
}

 So now your viewDidLoad function will look like this:


func viewDidLoad() {
    super.viewDidLoad()
    //Add a compose button alongside the existing left bar button item from the storyboard.
    let email = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: Selector("someAction"))
    if let leftBarButtonItems = navigationItem?.leftBarButtonItems {
        navigationItem?.leftBarButtonItems = leftBarButtonItems + [email]
    }
}

Now you can write a method for your added button like this:


@IBAction func someAction() {
    // Your code here...
}

Further Reading:

How to add four or more UIBarButtonitem at navigation bar with custom image through code?

Hope it helps,

Thanks & Regards
Mandar Apte

Comments

One response to “How to add four or more navigation bar buttons in your iOS App XCode Project”

  1. Mojtaba Avatar
    Mojtaba

    Thanks very much, I have three items on my navigation bar, two buttons on the left and right side of it and an image in the center. I followed your guide but it added the fourth button on the right side of the left button. I wanted to ask if you can help me on get it to the left side of the left button. And if you can also help me on setting a texture for this fourth button.
    Thanks very much for your help.

Leave a Reply

Sponsors


Search