Add Navigation Bar Button Item in Tab Bar View Controller

I have created Tab Bar view controller with three views. I have also embedded same tab bar in Navigation Bar Controller.


Now I want to add Navingation Bar Button item at right side. When I try to add button in IB individually three view controller doesn't accept it.


How can I add Navingation bar button item at right side?

Just create the new viewController in the canvas of IB.

Then

- control drag from the initial TabBarController to the new viewController,

- select View controllers in the popup.

A new item will appear at the right of the tabBar.


Note: control drag from the view content or the Tab Bar Controller icon at bthe top left of the TabBarController, not from the tabBar itself.

You gave me wrong answer....


I want to add Navigation Bar Button & Not Tab Bar View Controller...

OK. See your title:


Add Navigation Bar Button Item in Tab Bar View Controller


Isn't it a bit misleading ?


So, with your further explanation:


In one of the controllers where you navigate to, add the following:

   @objc func addButtonAction(_ sender: UIBarButtonItem) {
        print("Test Right button", self.navigationItem.title)
    } 
  
    var addButton: UIBarButtonItem!

Then, in viewDidLoad, add:


       addButton = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addButtonAction(_:)))
        self.navigationItem.rightBarButtonItem = self.addButton

No It's not working... Button is not appearing.


Keep in mind... I have embeded Navigation Bar to Tab Bar view Controller.

I have three ViewControllers in a UITabBarViewController, say VC1, VC2 and VC3.

I made all three of them extend a base UIViewController say BaseVC.


class VC1: BaseVC{

...

}


class VC2: BaseVC{

...

}


class VC3: BaseVC{

...

}


in the BaseVC class write the following:


class BaseVC: UIViewController {


override func viewDidLoad() {

super.viewDidLoad()

let moreOptions = UIBarButtonItem(image: UIImage(systemName: K.UI.moreOptions), style: .plain, target: self, action: #selector(showMoreOption))


self.tabBarController?.navigationItem.rightBarButtonItems = [moreOptions]

}


@objc func showMoreOption(){

print("Mor Options Clicked"

}


}

Add Navigation Bar Button Item in Tab Bar View Controller
 
 
Q