How to make an UIPickerView with a Done button?

前端 未结 5 1778
天涯浪人
天涯浪人 2020-12-22 16:31

I am having difficulties to make an UIPickerView with a done button to appear when the users taps a UITextField. This is my code so far. Everything builds fine, but when I t

相关标签:
5条回答
  • 2020-12-22 17:14

    People seem to be using the code here to other questions to ask the same thing that VBaarathi asked

    In Swift3

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
    

    Then you can put the following in the same swift file

    func donePicker (sender:UIBarButtonItem)
    {
         // Put something here
    }
    
    0 讨论(0)
  • 2020-12-22 17:20

    I think it's better to make a custom class to remove code from controller.

    Swift 4/5 example:

    import Foundation
    import UIKit
    
    protocol ToolbarPickerViewDelegate: class {
        func didTapDone()
        func didTapCancel()
    }
    
    class ToolbarPickerView: UIPickerView {
    
        public private(set) var toolbar: UIToolbar?
        public weak var toolbarDelegate: ToolbarPickerViewDelegate?
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.commonInit()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.commonInit()
        }
    
        private func commonInit() {
            let toolBar = UIToolbar()
            toolBar.barStyle = UIBarStyle.default
            toolBar.isTranslucent = true
            toolBar.tintColor = .black
            toolBar.sizeToFit()
    
            let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.doneTapped))
            let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
            let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(self.cancelTapped))
    
            toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
            toolBar.isUserInteractionEnabled = true
    
            self.toolbar = toolBar
        }
    
        @objc func doneTapped() {
            self.toolbarDelegate?.didTapDone()
        }
    
        @objc func cancelTapped() {
            self.toolbarDelegate?.didTapCancel()
        }
    }
    

    Usage example:

    class MyViewController: UIViewController {
    
        @IBOutlet weak var textField: UITextField!
        fileprivate let pickerView = ToolbarPickerView()
        fileprivate let titles = ["0", "1", "2", "3"]
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.textField.inputView = self.pickerView
            self.textField.inputAccessoryView = self.pickerView.toolbar
    
            self.pickerView.dataSource = self
            self.pickerView.delegate = self
            self.pickerView.toolbarDelegate = self
    
            self.pickerView.reloadAllComponents()
        }
    }
    
    extension MyViewController: UIPickerViewDataSource, UIPickerViewDelegate {
    
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return self.titles.count
        }
    
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
    
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            return self.titles[row]
        }
    
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            self.textField.text = self.titles[row]
        }
    }
    
    extension MyViewController: ToolbarPickerViewDelegate {
    
        func didTapDone() {
            let row = self.pickerView.selectedRow(inComponent: 0)
            self.pickerView.selectRow(row, inComponent: 0, animated: false)
            self.textView.text = self.titles[row]
            self.textField.resignFirstResponder()
        }
    
        func didTapCancel() {
            self.textField.text = nil
            self.textField.resignFirstResponder()
        }
    }
    
    0 讨论(0)
  • 2020-12-22 17:24

    Swift 3x:

    The awesome answer is already given but here is the different approach, just make a pickerView and give the inputView and inputAccessoryView like this:

    YOUR_TEXTFIELD_NAME.inputView = picker
    
    
    func addKeyboardToolBar() {
    
      var nextButton: UIBarButtonItem?
      var keyboardToolBar = UIToolbar(frame: CGRect(x: CGFloat(0), y: 
      CGFloat(0), width: CGFloat(pickerView.frame.size.width), height: CGFloat(25)))
      keyboardToolBar.sizeToFit()
      keyboardToolBar.barStyle = .default
      daysHourTextField.inputAccessoryView = keyboardToolBar
      nextButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.textFieldShouldReturn))
      keyboardToolBar.items = [UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), nextButton]
    
    }
    
    0 讨论(0)
  • 2020-12-22 17:26

    Add this code to your viewDidLoad() instead of method

    let picker: UIPickerView
    picker = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
    picker.backgroundColor = .whiteColor()
    
    picker.showsSelectionIndicator = true
    picker.delegate = self
    picker.dataSource = self
    
    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
    toolBar.sizeToFit()
    
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(self. donePicker))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(self. donePicker))
    
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.userInteractionEnabled = true
    
    textField1.inputView = picker
    textField1.inputAccessoryView = toolBar
    
    0 讨论(0)
  • 2020-12-22 17:29

    UIPickerView with a Done button? Swift4

    Step 1 : Adding one textFiled named txt_pickUpData in ViewController and give IBOutlet connection and delegate. also take one UIDatePicker variable. and take an Array with string value which is display on picker wheel.

    @IBOutlet weak var txt_pickUpData: UITextField!
    var myPickerView : UIPickerView!
    var pickerData = ["Hitesh Modi" , "Kirit Modi" , "Ganesh Modi" , "Paresh Modi"]
    

    Step 2 : Also adding the delegate of UIPickerView and UITextFiled.

    class ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate{
    

    Step 3 : The function pickUp to create UIPickerView with ToolBar.

    func pickUp(_ textField : UITextField){
    
    // UIPickerView
    self.myPickerView = UIPickerView(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))
    self.myPickerView.delegate = self
    self.myPickerView.dataSource = self
    self.myPickerView.backgroundColor = UIColor.white
    textField.inputView = self.myPickerView
    
    // ToolBar
    let toolBar = UIToolbar()
    toolBar.barStyle = .default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
    toolBar.sizeToFit()
    
    // Adding Button ToolBar
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.doneClick))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ViewController.cancelClick))
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
    textField.inputAccessoryView = toolBar
    
     }
    

    Step 4 : Adding the delegate and datasource methods of UIPickerView to display data on UIPickerView wheel.

    //MARK:- PickerView Delegate & DataSource
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
     }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
     }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
      }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.txt_pickUpData.text = pickerData[row]
     }
     //MARK:- TextFiled Delegate
    
     func textFieldDidBeginEditing(_ textField: UITextField) {
    self.pickUp(txt_pickUpData)
     }
    

    Step 5 : Adding two buttons method which which is in ToolBar. One is doneClick and other is cancelClick. Which is dismiss the UIPickerView.

    func doneClick() {
      txt_pickUpData.resignFirstResponder()
     }
    func cancelClick() {
      txt_pickUpData.resignFirstResponder()
    }
    

    Step 6 : Calling the pickUp function in UITextField delegate method.

    func textFieldDidBeginEditing(_ textField: UITextField) {
    self.pickUp(txt_pickUpData)
    }
    

    Just copy and paste in Your code.

    0 讨论(0)
提交回复
热议问题