Reputation: 1085
I am making an app without using Interface Builder, so I'm creating everything by code.
I have a view controller that is embedded in a navigation controller. I know navigation controllers have a toolbar property, and all I need to do is set the boolean to false to unhide it. My code looks like this (it is a function called in viewDidLoad):
func setUpToolBar(){
navigationController?.toolbarHidden = false
navigationController?.toolbar.barTintColor = UIColor.blackColor()
navigationController?.toolbar.tintColor = UIColor.whiteColor()
toolItems.append(addImageButton)
toolItems.append(UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil))
toolItems.append(exportImageButton)
toolItems.append(UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil))
toolItems.append(deleteImageButton)
navigationController?.toolbar.setItems(toolItems, animated: false)
}
Here is where and how I am declaring these properties for my view controller:
private var addImageButton : UIBarButtonItem = {
let button = UIFactory.createBarButtonItem(nil, imageNamed: "plus button", style: .Plain, target: ImagesCollectionViewController.self, selector: #selector(ImagesCollectionViewController.addImage))
return button
}()
private var exportImageButton : UIBarButtonItem = {
let button = UIFactory.createBarButtonItem(nil, imageNamed: "export button", style: .Plain, target: ImagesCollectionViewController.self, selector: #selector(ImagesCollectionViewController.exportImagePressed))
return button
}()
private var deleteImageButton : UIBarButtonItem = {
let button = UIFactory.createBarButtonItem(nil, imageNamed: "trash button", style: .Plain, target: ImagesCollectionViewController.self, selector: #selector(ImagesCollectionViewController.deleteImagePressed))
return button
}()
private var toolItems : [UIBarButtonItem] = []
Here is the function for my UIFactory class which simply handles the hassle of creating these UI items outside of the view controller
class func createBarButtonItem(title: String?, imageNamed : String?, style : UIBarButtonItemStyle?, target : AnyObject?, selector : Selector?) -> UIBarButtonItem{
let button = UIBarButtonItem()
if let theTitle = title {
button.title = theTitle
}
if let theImageName = imageNamed {
button.image = UIImage(named: theImageName)
}
if let theStyle = style{
button.style = theStyle
}
if let theTarget = target {
button.target = theTarget
}
if let theSelector = selector {
button.action = theSelector
}
return button
}
And, after running this code in the simulator, this is what I see (no toolbar items! Also, notice I have a bar button item in that navigation bar, which I added in code just fine)
Please, someone help me. I am stuck on such a nonsensical step and I can't proceed with my app without this.
Upvotes: 4
Views: 5567
Reputation: 439
I changed
navigationController!.toolbar.setItems(toolItems, animated: true)
to
toolbarItems = toolItems
and worked for me.
Upvotes: 2
Reputation: 215
Just remove
navigationController?.toolbar.setItems(toolItems, animated: false)
And put this line of code
self.toolbarItems = [toolItems]
Upvotes: 9