UIBarButtonItems不能以编程方式创buildUIToolbar后显示?
我遇到了这个问题,我似乎无法find在线答案。 基本上我想要做的是用一些UIBarButtonItems
以编程方式创build一个UIToolbar
。
我所做的(如下所述)是创buildUIToolbar
,然后将UIToolbar
的项目设置为一个数组,其中包含所有我想要的UIBarButtonItems
。
不幸的是,尽pipeUIToolbar
显示出来了, UIBarButtonItems
还没有显示出来
任何build议或解释,为什么这是发生了非常感谢!
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) }
正如你所看到的只有工具栏显示
并且图像名称(用于创buildUIBarButton的图像)与Assets目录中的图像名称相同
我会想象这个问题是你创buildUIToolbar
的奇怪方式。 这条线似乎深感怀疑:
let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))
为什么使它成为你视图高度的0.7倍?
要解决这个问题,创build工具栏或者没有框架构造函数或者CGRectZero
框架。 然后使用sizeToFit()
使其成为适当的大小。
如果可能,更简单的方法是使用UINavigationController
并告诉它显示自己的工具栏。 然后,您可以通过将视图控制器的toolbarItems
属性设置为您的数组来填充该视图,并且它们都可以工作,而无需您创build,定位或pipe理UIToolbar
。