问题描述
我遇到了这个问题,我似乎无法在网上找到答案.基本上我想要做的是以编程方式创建一个带有一些 UIBarButtonItems
的 UIToolbar
.
I'm running across this issue which I can't seem to find the answer to online. Basically what I'm trying to do is programmatically create a UIToolbar
with some UIBarButtonItems
.
我所做的(如下详述)是创建UIToolbar
,然后将UIToolbar
的项目设置为一个包含所有UIBarButtonItems
我想要.
What I've done (as detailed below) is create the UIToolbar
and then set the items of the UIToolbar
to an array holding all the UIBarButtonItems
I want.
不幸的是,尽管 UIToolbar
出现了,UIBarButtonItems
还没有出现
Unfortunately, though the UIToolbar
shows up, the UIBarButtonItems
have yet to show themselves
非常感谢任何有关为什么会发生这种情况的建议或解释!
Any suggestions or explanations as to why this is happening is much appreciated!
class DrawViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//create bar buttons
let deleteBarButton = UIBarButtonItem(image: UIImage(named: "greyDelete"), style: .Plain, target: self, action: "deleteView:")
let eraseBarButton = UIBarButtonItem(image: UIImage(named: "greyErase"), style: .Plain, target: self, action: "erase:")
let resizeBarButton = UIBarButtonItem(image: UIImage(named: "greyResize"), style: .Plain, target: self, action: "resize:")
let viewBarButton = UIBarButtonItem(image: UIImage(named: "greyView"), style: .Plain, target: self, action: "view:")
let colorBarButton = UIBarButtonItem(image: UIImage(named: "greyColor"), style: .Plain, target: self, action: "color:")
let drawBarButton = UIBarButtonItem(image: UIImage(named: "greyDraw"), style: .Plain, target: self, action: "draw:")
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
//set up toolbar
let toolbarItems = [deleteBarButton, flexibleSpace, eraseBarButton,
flexibleSpace, resizeBarButton, flexibleSpace,
viewBarButton, flexibleSpace, colorBarButton,
flexibleSpace, drawBarButton]
let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))
toolbar.barTintColor = UIColor(patternImage: UIImage(named: "blueToolbar")!)
toolbar.setItems(toolbarItems, animated: true)
self.view.addSubview(toolbar)
}
图像名称(用于创建 UIBarButtons 的图像)与那些名称相同在资产目录中
推荐答案
我认为问题在于您创建 UIToolbar
的奇怪方式.这里的这条线似乎很可疑:
I would imagine the problem is the curious way you're creating the UIToolbar
. This line here seems deeply suspect:
let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))
为什么将其设为视图高度的 0.7 倍?
Why make it 0.7 times the height of your view?
要解决此问题,请在不使用框架构造函数或使用 CGRectZero
为其框架创建工具栏.然后使用 sizeToFit()
使其成为合适的大小.
To fix this problem, create the toolbar either without the frame constructor or with CGRectZero
for its frame. Then use sizeToFit()
to make it the appropriate size.
如果可能,一个更简单的替代方法是使用 UINavigationController
并告诉它显示自己的工具栏.然后,您可以通过将视图控制器的 toolbarItems
属性设置为您的数组来填充它,而无需您创建、定位或管理 UIToolbar
即可完成所有工作.
A simpler alternative, if possible, is to use a UINavigationController
and tell it to show its own toolbar. You can then fill that by setting your view controller's toolbarItems
property to your array, and it will all work without you having to create, position or manage a UIToolbar
at all.
这篇关于以编程方式创建 UIToolbar 后未显示 UIBarButtonItems?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!