Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new bar button in Navigation Bar with a Tab Bar Controller Swift

I have the next structure in swift 3:

Navigation Controller -> Tab Bar Controller -> 4 View Controllers

I have 3 commons bar buttons items. My problem is, in just one of the view Controllers I want to add new bar button item on the top-right corner. This way when I navigate to the others views the new button should not appear.

How can I solve this?

Thanks!

like image 583
Pablo Garcia Avatar asked Oct 19 '25 04:10

Pablo Garcia


1 Answers

You should set it with tabBarController like this in Swift:

let button = UIButton(type: .custom)
button.setImage(UIImage(named: "imageName"), for: .normal)
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
let leftButtonBar = UIBarButtonItem(customView: button)
tabBarController?.navigationItem.rightBarButtonItems = [leftButtonBar]. 

this is best answer forever and works correctly.

like image 62
reza_khalafi Avatar answered Oct 21 '25 17:10

reza_khalafi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!