2

I updated my project to Swift 4 XCode 9 I have a InputText whit a input view using a Picker I added a UIToolBar at InputText. It was working well before upgrade. now it's like the UIPickerView it's over the UIToolBar.

I can see but if i click on it's like I click on UIPickerView

The UIToolBar it's perfectly working, because if i set the toolbar to a normal TextInput it appear and work well..

What can be?

Here is my code:

var picker: UIPickerView
picker = UIPickerView(frame: CGRect(x: 0, y: 200, width: view.frame.width, height: 300))
picker.backgroundColor = .white

picker.showsSelectionIndicator = true
picker.delegate = self
picker.dataSource = PickerData as? UIPickerViewDataSource


textField.inputView = picker

let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = .blue
toolBar.sizeToFit()

let doneButton = UIBarButtonItem(title: "Seleziona", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneFunction(_:)))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Annulla", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneFunction(_:)))

toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true

textField.inputAccessoryView = toolBar

I tried also with picker.addSubview(toolBar) but whit same result.

Piedecess
  • 23
  • 5

1 Answers1

0

I had the same issue and I solved it with a background view and adding 2 subviews on it - picker and toolbar. Here is the code:

    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = green
    toolBar.sizeToFit()

    picker = UIPickerView(frame: CGRect(x: 0, y: toolBar.frame.size.height, width: view.frame.width, height: 300))
    picker.tintColor = green
    picker.showsSelectionIndicator = true
    picker.delegate = self
    picker.dataSource = self

    let bgView = UIView(frame: CGRect(x: 0, y: 200, width: view.frame.width, height: 300 + toolBar.frame.size.height))

    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(nextPicker))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelPicker))
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    bgView.addSubview(picker)
    bgView.addSubview(toolBar)
    cell.textField.inputView = bgView

Hopefully it helped.

Martin Mikusovic
  • 1,002
  • 2
  • 9
  • 14