Ali Alebrahim Ali Alebrahim - 5 months ago 12
iOS Question

Bar button item is not being added

Im facing a problem, i have a

viewController
that has both
navigationController
and
tabBarController
. Whenever in code i try to add a right or left
barButtonItem
or even to add a title to the
navigationBar
nothing happens. Is it because of the tab bar? or what might the problem be

in
viewDidLoad


let addButton = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: #selector(ProfileVC.addNewService))
navigationItem.rightBarButtonItem = addButton


here how the view controller look in the storyboard

enter image description here

Answer

try:

parentViewController?.navigationItem.rightBarButtonItem = addButton

since your TabBarController is embedded in NavigationController like this:

UINavigationController -> UITabBarcontroller -> YourViewController