MY Scenario, I am trying to create button click to open UIPickerView and selecte picker data to show on button title using swift. Here, Toolbar also I would like to add into the pickerview controller.
let button = UIButton(type: .custom)
button.setTitle("",for: .normal)
button.frame = CGRect(x: CGFloat(amount_textfield.frame.size.width - 9), y: CGFloat(5), width: CGFloat(9), height: CGFloat(20))
button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
amount_textfield.leftView = button
amount_textfield.leftViewMode = .always
@IBAction func refresh(_ sender: Any) {
// Here, I need to execute picker view
}
There are few steps that you have to follow:
Create initially hidden
UIPickerViewand set itsdelegateanddataSourceHave data source array which you use for this picker view
Unhide your picker view when button is pressed
Integrate picker view's delegate method
pickerView(_:didSelectRow:inComponent:). When row is selected, set title of button as element from your data source array at index as selectedrowOptional: Hide picker view