I made this extension to close the collector. Swift 2
extension UIToolbar { func ToolbarPiker(mySelect : Selector) -> UIToolbar { let toolBar = UIToolbar() toolBar.barStyle = UIBarStyle.Default toolBar.translucent = true toolBar.tintColor = UIColor.blackColor() toolBar.sizeToFit() let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: mySelect) let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) toolBar.setItems([ spaceButton, doneButton], animated: false) toolBar.userInteractionEnabled = true return toolBar } }
Swift 3 - 4
extension UIToolbar { func ToolbarPiker(mySelect : Selector) -> UIToolbar { let toolBar = UIToolbar() toolBar.barStyle = UIBarStyle.default toolBar.isTranslucent = true toolBar.tintColor = UIColor.black toolBar.sizeToFit() let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: mySelect) let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) toolBar.setItems([ spaceButton, doneButton], animated: false) toolBar.isUserInteractionEnabled = true return toolBar } }
And you can use it simply in your viewController:
override func viewDidLoad() { super.viewDidLoad() //SWIFT2 /* let toolBar = UIToolbar().ToolbarPiker(
Rob
source share