navigationItem button not showing

2019-08-12 16:44发布

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.

1条回答
Explosion°爆炸
2楼-- · 2019-08-12 16:48

Now the problem is i added them correctly, they're functional and working 100%.

but they're not showing on the UINavigationBar.

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 for UIBarButtonItem would be valid, like this:

// right
let rightButton = UIButton(type: .custom)
rightButton.setTitle("Button.Done".localized, for: .normal)
rightButton.addTarget(self, action: #selector(saveButtonTapped), for: .touchUpInside)
let rightBarButtonItem = UIBarButtonItem(customView: rightButton)
navigationItem.setRightBarButton(rightBarButtonItem, animated: true)

// left
let leftButton = UIButton(type: .custom)
leftButton.setTitle("Button.Cancel".localized, for: .normal)
leftButton.addTarget(self, action: #selector(cancelButtonTapped), for: .touchUpInside)
let leftBarButtonItem = UIBarButtonItem(customView: leftButton)
navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)

Therefore, for any needed UI update, you could edit rightButton and leftButton, example:

leftButton.layer.backgroundColor = UIColor.red.cgColor
leftButton.layer.borderWidth = 2.0
leftButton.layer.borderColor = UIColor.black.cgColor


In addition, I would assume that there is no need to call:

self.navigationItem.setHidesBackButton(isBackHidden, animated: false)

When setting the left bar button item, it should be a replacement for the back button by default.

查看更多
登录 后发表回答