在Swift中以编程方式向工具栏添加按钮

23

我在Swift中添加一个按钮到工具栏有些困难,下面您可以看到我想要的工具栏的图像。不幸的是,即使我已经在Storyboard文件中设计好了它,但在将工具栏设置为可见时它并没有显示出来。

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

enter image description here

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

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:")
)

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

但是,工具栏仍然是空的,为什么呢?

6个回答

45

以上方法都对我不起作用,但是:

Swift 3 / Swift 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

5
可以的,这段话的翻译如下:这个有效,但真正的问题是其他人在做什么? - Allison
那些往下滚动的人将会得到奖励。谢谢。 - biomiker
这应该是被接受的答案,但是是什么让它与被接受的答案不同呢? - Takasur

23
通常的做法是创建工具栏项目数组,然后将该数组分配给工具栏的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

如果操作为nil,为什么需要将目标设置为self? - BangOperator
正如 @DavidSeek 所提到的,UIViewController 上的 self.toolbarItems 是设置工具栏按钮的正确方式。 - Yassine ElBadaoui
哦,我刚刚知道navigationController有一个内置的“工具栏”,之前我自己创建了一个“工具栏”并在程序中设置了它的约束,真是错过了一个好机会 :) - Zhou Haibo

7
self.navigationController?.toolbarItems = items

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

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

试试看。

        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)

2
这是一个关于MKUserTrackingBarButtonItem的示例:
navigationController?.toolbarHidden = false
let barButtonItem = MKUserTrackingBarButtonItem(mapView: self.mapView)
self.toolbarItems = [barButtonItem]

-1
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)

-1

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

Swift 3

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

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

将"ThisViewController.onDoneBarButtonClick"替换为您的视图控制器类名称和您想编写以管理工具栏按钮单击的任何方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接