UIBarButtonItem 未显示在 UIToolbar Swift 上 [英] UIBarButtonItem not showing up on UIToolbar Swift

查看:38
本文介绍了UIBarButtonItem 未显示在 UIToolbar Swift 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

This is intended functionality. According to the docs:

与导航控制器关联的自定义工具栏.(只读)

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

该属性包含对由管理的内置工具栏的引用导航控制器.仅提供对该工具栏的访问对于想要从工具栏中显示操作表的客户.你不应直接修改 UIToolbar 对象.管理这工具栏的内容是通过自定义视图控制器完成的与此导航控制器相关联.对于每个视图控制器在导航堆栈上,您可以分配一组自定义的工具栏项目使用 UIViewController 的 setToolbarItems:animated: 方法.

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.

这个工具栏的可见性是由toolbarHidden控制的财产.工具栏也遵守 hidesBottomBarWhenPushed 属性当前可见的视图控制器并隐藏和显示自身根据需要自动.

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

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

这篇关于UIBarButtonItem 未显示在 UIToolbar Swift 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆