gpt4 book ai didi

xcode - UIBarButtonItem 没有出现在 UIToolbar Swift 上

转载 作者:行者123 更新时间:2023-11-28 05:33:31 25 4
gpt4 key购买 nike

我有一个启用了工具栏的导航 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com