Search code examples
iosswiftuibuttonuipickerview

Add buttons to UIPickerView - Swift 1.2


I would like to add two UIButtons in the UIPickerView (on top of it). Please take a look at the Cancel and Done buttons in this image:

enter image description here

How can I do it?


Solution

  • I have solved this, answer in Swift:

    var pickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
    pickerView.backgroundColor = .whiteColor()
    pickerView.showsSelectionIndicator = true
    
    var toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
    toolBar.sizeToFit()
    
    
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Bordered, target: self, action: "donePicker")
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, target: self, action: "canclePicker")
    
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.userInteractionEnabled = true
    
    textField.inputView = pickerView
    textField.inputAccessoryView = toolBar