简体   繁体   中英

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!

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.

You can have the below structure by which you can have individual navigation to add button for each view controller

Tab Bar Controller ->Navigation Controller -> 1st View Controllers
                    ->Navigation Controller -> 2nd View Controllers
                    ->Navigation Controller -> 3rd View Controllers
                    ->Navigation Controller -> 4th View Controllers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM