0

I'm not sure why this piece of code which is supposed to embed two bar button items in a navigation controller's toolbar won't work. The toolbar itself is visible when I run my code, but not the bar button items. What am I doing wrong here? Thanks for attention.

class NavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Tool bar appearance
        toolbar.barTintColor = UIColor.blackColor()


        //Show tool bar by default
        self.navigationController?.toolbarHidden = false

        //Icons all located in images.xcassets
        let homeImage = UIImage(named: "home")
        let gameImage = UIImage(named: "logo")

        var toolBarItems = [UIBarButtonItem]()

        let homeButton = UIBarButtonItem(image: homeImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(NavigationController.toHome))
        homeButton.title = "Home"


        let gameButton = UIBarButtonItem(image: gameImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(NavigationController.toGame))
        homeButton.title = "Game"

        //Place the bar items in toolBarItems array
        toolBarItems.append(homeButton)
        toolBarItems.append(gameButton)

        //self.toolbar.items = toolBarItems
        self.toolbar.setItems(toolBarItems, animated: true)

    }//End viewDidLoad


    func toHome() {
        let homeVC = HomeViewController(nibName: "HomeViewController", bundle: nil)
        self.pushViewController(homeVC, animated: true)
    }

    func toGame() {
        let gameVC = GameViewController(nibName: "GameViewController", bundle: nil)
        self.pushViewController(gameVC, animated: true)
    }

}
Nguzzi Man
  • 59
  • 7

3 Answers3

1

Did you create a second .swift file for your dependent controller? You should move this code to the dependant controller file

self.navigationController?.toolbarHidden = false

    let button1 = UIBarButtonItem(title: "home", style: .Plain, target: self, action: #selector(SecondViewController.home))

    let myToolBar = [button1]

    self.setToolbarItems(myToolBar, animated: true)
nadi9
  • 576
  • 3
  • 11
  • and yes. You can do with toolbar.hidden = false as it was said earlier. Thank you! I will correct my code as well – nadi9 Aug 21 '16 at 19:42
  • not sure what you mean by the "dependant controller" – Nguzzi Man Aug 23 '16 at 04:06
  • I meant. I have file for each controller in my project. One .swift for navigation controller and another one for its defendant ( e.g. Table view controller or view controller) – nadi9 Aug 23 '16 at 05:41
  • I do have a separate .swift file for each view controller and one for the navigation controller. The tool bar is visible in each of these view controllers, but the bar buttons won't show up. – Nguzzi Man Aug 24 '16 at 06:47
0

I am not sure but I think your buttons has size 0. So maybe you should add some constraints or view frame size. You can try debugging using the view hierarchy debugger.

TomCobo
  • 2,886
  • 3
  • 25
  • 43
0

Maybe you just have to replace this self.navigationController?.toolbarHidden = false With this toolbarHidden = false

I'm confused by your code. Is the class you show us, the one Navigation Controller where all other ViewController depend on or is it itself one dependent ViewController that in this case appears to be a Navigation Controller? Or is it a Navigation Controller by mistake? (not very likely)

Why I ask? At one time you're referring to the parent navigation controller with self.navigationController?.toolbarHidden = false which is not this navigation controller itself. Then in the rest of the code, you refer to this controller itself.

Hope this leads to the right thinking.

jboi
  • 11,324
  • 4
  • 36
  • 43
  • The class is is indeed the one navigation controller which all the other view controllers depend on. From what I understand about navigation controllers, they have an inherent navigation bar and a tool bar, into which bar button items can be embedded. What I'm trying to do is embed bar buttons onto the tool bar that belongs to this navigation controller, which should ultimately be visible to all the view controllers that are children of the navigation controller. – Nguzzi Man Aug 21 '16 at 18:15
  • I do understand now why it should just be toolbarHidden = false I fixed that one line, but still now change. Toolbar remains visible but bar buttons don't show up – Nguzzi Man Aug 21 '16 at 18:28
  • I usually let storyboard do all these things. In these cases, it automatically defines the rootVieController. Do you have one? Defined in storyboard or programmatically? Chances are, that the NavigationController presents and layouts everything only with this root controller present. – jboi Aug 21 '16 at 20:50
  • no the app is purely programatic. There are just so many moving parts, I decided early on not to go storyboard – Nguzzi Man Aug 22 '16 at 04:01
  • Sounds like, you should add the rootViewController and see what happens then. – jboi Aug 22 '16 at 08:20
  • Try to drag Navigation Controller from object library and drop to the mainboard. You will see what we are talking about – nadi9 Aug 23 '16 at 08:02