我很难快速地向工具栏添加一个按钮,下面你可以看到我想要的工具栏的图像,不幸的是,即使我在故事板文件中设计了它,它也不会显示在将工具栏设置为可见。
我设计的方式有两个项目,第一个是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 添加到该数组中。
然而,工具带仍然是空的,这是为什么呢?
以上都不适合我,但是:
斯威夫特 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)