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






Leave a Reply