I have a UINavigationItem
i am trying to show a UIBarButtonItem
.
Now the problem is i added them correctly, they're functional and working 100%.
but they're not showing on the UINavigationBar
.
Now for the flow I am doing the following.
1- I am hiding the back button, like this
self.navigationItem.setHidesBackButton(isBackHidden, animated: false)
2- I am adding those 2 buttons using a function dynamically at run time, when a user tap on a UIButton
.
let rightBarItem = UIBarButtonItem(title: "Button.Done".localized, style: .plain, target: self, action: #selector(self.saveButtonTapped))
navigationItem.rightBarButtonItem = rightBarItem
let leftBarItem = UIBarButtonItem(title: "Button.Cancel".localized, style: .plain, target: self, action: #selector(self.cancelButtonTapped))
navigationItem.rightBarButtonItem?.tintColor = .red // still nothing
navigationItem.leftBarButtonItem = leftBarItem
3- I have tried to use those functions and still the same result
self.navigationItem.setLeftBarButton(UIBarButtonItem?, animated: Bool)
self.navigationItem.setRightBarButton(UIBarButtonItem?, animated: Bool)
Summary :
I have tried to change the tintColor
as they're functionally working i thought it was a color problem.
I have tried to use them inside DispatchQueue.main.async {}
thinking it might be a thread problem since it dynamic.
I have debugged and check for the items in the UINavigationItem
and they're exist.
What is going on mainly:
The buttons are not shown but they are working just fine when tapped on their places on the UINavigationItem
.
Based on what you mentioned, it is just a UI issue -for unknown reason-.
So what you could do to confirm that there are button items will be shown in the navigation item is to let the bar button item to has a custom view. Adding a
UIButton
as a custom view forUIBarButtonItem
would be valid, like this:Therefore, for any needed UI update, you could edit
rightButton
andleftButton
, example:In addition, I would assume that there is no need to call:
When setting the left bar button item, it should be a replacement for the back button by default.