简体   繁体   中英

Button item not shown in navigationBar embebed in TabBarController

I'm working on an app where a viewController is embebed in navigationBar and this navigationBar is embebed in TabBarViewController.

In my app I have 2 different tabs (for the moment), one tab is the "Activity view". This is a viewController with a tableView where list activities. Every cell will show the same view controller showing detail activity when it's touched.

My problem is that not showing navigationItem with buttons.

I'm trying the solution of: Bar button item not shown in navigationBar , because my problem is very similar but it did not work.

some screenshots of storyboard and app running: storyboard App running without buttons

I'm trying programmatically add buttons but no results:

var logoutButton: UIBarButtonItem!
var filterButton: UIBarButtonItem!


//MARK: - Lyfe cycle
override func viewDidLoad() {
super.viewDidLoad()

let logoutImage = UIImage(named: "logout-24")
let filterImage = UIImage(named: "filterOutline-24")

logoutButton = UIBarButtonItem(image: logoutImage, style: .Plain, target: self, action: "logoutAction:")
filterButton = UIBarButtonItem(image: filterImage, style: .Plain, target: self, action: "filterAction:")
navigationItem.rightBarButtonItems = [filterButton, logoutButton]

Check your image. I don't see any problem with your code. Or you can try add just title like below

logoutButton = UIBarButtonItem(title: "Log out", style: .Plain, target: self, action:Selector("logoutAction:"))
filterButton = UIBarButtonItem(title: "Filter", style: .Plain, target: self, action: Selector("filterAction:"))

If button bar show. I sure that problem is your image.

I also faced the similar problem, later I found that set up your navigation bar in tab bar controller itself, no need to do in view controllers

let profilePutton = UIButton(type: .custom)
profilePutton.setTitle("Profile", for: .normal)
profilePutton.frame = CGRect(x: 0.0, y: 0.0, width: 30.0, height: 30.0)
profilePutton.addTarget(self, action: #selector(openProfilePage), for: .touchUpInside)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: profilePutton)

For tabar application we should add like this,

 let logoutButton = UIBarButtonItem(image: UIImage(named: "ic_logout"), style: .plain, target: self, action:#selector(logoutButtonTapped))    
 self.tabBarController?.navigationItem.leftBarButtonItem = logoutButton

 let nightModeButton = UIBarButtonItem(image: UIImage(named: "ic_night_mode_off"), style: .plain, target: self, action:#selector(nightModeButoonTapped))
 self.tabBarController?.navigationItem.rightBarButtonItem  = nightModeButton

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