ios 快速以编程方式向工具栏添加按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35106022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:23:02  来源:igfitidea点击:

Adding buttons to toolbar programmatically in swift

iosswift

提问by Hobbyist

I'm having a hard time adding a button to the toolbar in swift, below you can see an image of the toolbar that I'm after, unfortunately even though I have it designed in my Storyboard file, it doesn't show up when setting the toolbar to be visible.

我很难在 swift 中向工具栏添加一个按钮,在下面你可以看到我想要的工具栏的图像,不幸的是,即使我在我的 Storyboard 文件中设计了它,它也没有出现将工具栏设置为可见。

The way that I have designed this is two items, the first being a flexable spaceelement, and the second being an addelement. It looks like this:

我设计它的方式是两个项目,第一个是一个flexable space元素,第二个是一个add元素。它看起来像这样:

enter image description here

在此处输入图片说明

Here's the code that I've used to attempt to replicate this in code:

这是我用来尝试在代码中复制它的代码:

self.navigationController?.toolbarHidden = false
self.navigationController?.toolbarItems = [UIBarButtonItem]()
self.navigationController?.toolbarItems?.append(
    UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
)
self.navigationController?.toolbarItems?.append(
    UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:")
)

As you can see I'm setting the toolbar to be visible, initializing (and clearing) the toolbarItems array of UIBarButtonItem, and then adding two UIBarButtonItem's to the array, in the proper order.

如您所见,我将工具栏设置为可见,初始化(并清除)UIBarButtonItem 的 toolbarItems 数组,然后以正确的顺序将两个 UIBarButtonItem 添加到该数组中。

However, the toolbelt remains empty, why is this?

但是工具带还是空的,这是为什么呢?

回答by David Seek

None of the above worked for me, but:

以上都不适合我,但是:

Swift 3 / Swift 4

斯威夫特 3 / 斯威夫特 4

self.navigationController?.isToolbarHidden = false

var items = [UIBarButtonItem]()

items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
items.append( UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(add)) ) // replace add with your function

self.toolbarItems = items // this made the difference. setting the items to the controller, not the navigationcontroller

回答by vadian

The usual way to do that is to create the array of toolbar items and then assign the array to the itemsproperty of the toolbar.

通常的方法是创建工具栏项的数组,然后将数组分配给items工具栏的属性。

self.navigationController?.isToolbarHidden = false
var items = [UIBarButtonItem]()
items.append(
    UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
items.append(
    UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(onClickedToolbeltButton(_:)))
)
toolbarItems = items

回答by Ulysses

self.navigationController?.toolbarItems = items

self.navigationController?.setToolbarItems(items, animated: false)

self.navigationController?.toolbar.setItems(items, animated: false)

Try it.

尝试一下。

        self.navigationController?.toolbarHidden = false
        var items = [UIBarButtonItem]()
        items.append(
            UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
        )
        items.append(
            UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:")
        )

        self.navigationController?.toolbar.setItems(items, animated: false)

回答by damianesteban

Here is an example with MKUserTrackingBarButtonItem:

这是一个示例MKUserTrackingBarButtonItem

navigationController?.toolbarHidden = false
let barButtonItem = MKUserTrackingBarButtonItem(mapView: self.mapView)
self.toolbarItems = [barButtonItem]

回答by Jorge Ramírez

Updated answer using the current selector syntax for

使用当前选择器语法更新答案

Swift 3

斯威夫特 3

var barButtons = [UIBarButtonItem]()
barButtons.append(
    UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(ThisViewController.onDoneBarButtonClick))
)
self.navigationItem.setRightBarButtonItems(barButtons, animated: false)

You can place this code in any loading event. It works seamless for me in viewDidLoad().

您可以将此代码放在任何加载事件中。它在 viewDidLoad() 中对我来说是无缝的。

Replace "ThisViewController.onDoneBarButtonClick" with your view controller class name and any method you want to write to manage the toolbar button click.

将“ThisViewController.onDoneBarButtonClick”替换为您的视图控制器类名以及您要编写的任何方法来管理工具栏按钮单击。

回答by Victor Carre?o

let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addSomething:")
toolbarItems = [UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil),addButton]
self.navigationController!.setToolbarHidden(false, animated: false)