1

I have a navigation controller with the toolbar enabled.I have a view controller with an MKMapView that is embedded in the navigation controller. When I run it in the simulator the toolbar shows up. However, when I try to add a UIBarButtonItem from code, the UIBarButtonItem doesn't show up on the toolbar. To add the button I have the following code in my viewDidAppear method:

var trackingButton:MKUserTrackingBarButtonItem = MKUserTrackingBarButtonItem(mapView: self.theMapView)
    self.navigationController?.toolbarItems?.append(trackingButton)
    //self.toolbarItems?.append(trackingButton) also doesn't work

Any Ideas on what is going wrong?

1 Answers1

2

This is intended functionality. According to the docs:

The custom toolbar associated with the navigation controller. (read-only)

This property contains a reference to the built-in toolbar managed by the navigation controller. Access to this toolbar is provided solely for clients that want to present an action sheet from the toolbar. You should not modify the UIToolbar object directly. Management of this toolbar’s contents is done through the custom view controllers associated with this navigation controller. For each view controller on the navigation stack, you can assign a custom set of toolbar items using the setToolbarItems:animated: method of UIViewController.

The visibility of this toolbar is controlled by the toolbarHidden property. The toolbar also obeys the hidesBottomBarWhenPushed property of the currently visible view controller and hides and shows itself automatically as needed.

In other words, you can't do navigationController.toolbar.setItems. You have to use the navigationControllers methods (i.e): navigationController.setToolbarItems

Oxcug
  • 6,524
  • 2
  • 31
  • 46