我在Swift中添加一个按钮到工具栏有些困难,下面您可以看到我想要的工具栏的图像。不幸的是,即使我已经在Storyboard文件中设计好了它,但在将工具栏设置为可见时它并没有显示出来。
我设计的方法是两个元素,第一个是flexable space
元素,第二个是add
元素。 它看起来像这样:
这是我用来尝试在代码中复制它的代码:
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。
但是,工具栏仍然是空的,为什么呢?