UIBarButtonItem not showing up on UIToolbar Swift

南笙酒味 提交于 2019-12-24 12:34:30

问题


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:


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



来源:https://stackoverflow.com/questions/26443452/uibarbuttonitem-not-showing-up-on-uitoolbar-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!