xcode - UIBarButtonItem 没有出现在 UIToolbar Swift 上

标签 xcode swift uibarbuttonitem

我有一个启用了工具栏的导航 Controller 。我有一个带有嵌入在导航 Controller 中的 MKMapView 的 View Controller 。当我在模拟器中运行它时,工具栏会出现。但是,当我尝试从代码添加 UIBarButtonItem 时,UIBarButtonItem 没有显示在工具栏上。要添加按钮,我的 viewDidAppear 方法中有以下代码:

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

有什么问题吗?

最佳答案

这是预期的功能。根据文档:

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.

换句话说,您不能执行 navigationController.toolbar.setItems。您必须使用 navigationControllers 方法(即):navigationController.setToolbarItems

关于xcode - UIBarButtonItem 没有出现在 UIToolbar Swift 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26443452/

相关文章:

ios - 无法安装 iOS 应用程序

ios - 有没有办法将 Swift 词典漂亮地打印到控制台?

ios - NSObject 传递给 JavaScriptCore 后未释放

ios - 快速为整个 iOS 应用程序使用自定义字体

swift - 使用参数从 swift 应用程序调用 shell

swift - 用户不允许定位时如何处理

iphone - 如何清除代码中的 NSLog 输出?

ios - 将 UIToolBar 添加到所有键盘(快速)

ios - 以编程方式设置图像的大小

iphone - 允许 UIBarButtonItem 触摸 UINavigationBar 的顶部和底部