I am trying to create a custom UIToolbar that I can add to multiple views, I don\'t want to do this programmatically so I am using xib to design a UIToolbar. Do I need to wrap
You can do this so:
In your root View Controller put this code in ViewDidLoad:
let toolBar = UINib(nibName: "ToolBar", bundle: nil).instantiateWithOwner(nil, options: nil).first as! ToolBar
toolBar.hidden = true
self.navigationController!.view.addSubview(toolBar)
self.navigationController!.toolbarItems = toolBar.items
In all View Controllers put in ViewDidLoad:
self.toolbarItems = self.navigationController!.toolbarItems
ToolBar.swift:
import UIKit
class ToolBar: UIToolbar {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBAction func item1Pressed(sender: AnyObject) {
print("item 1 pressed")
}
@IBAction func item2Pressed(sender: AnyObject) {
print("item 2 pressed")
}
@IBAction func item3Pressed(sender: AnyObject) {
print("item 3 pressed")
}
@IBAction func item4Pressed(sender: AnyObject) {
print("item 4 pressed")
}
}