我很难快速将按钮添加到工具栏,在下面您可以看到我正在寻找的工具栏图像,不幸的是,即使我已在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添加到该数组。
但是,工具带仍然是空的,为什么呢?
通常的方法是创建工具栏项目的数组,然后将该数组分配给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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句