Skip to content

Instantly share code, notes, and snippets.

@arpitjain03
Last active May 7, 2021 07:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpitjain03/c876e8fd3a7707f2b1b00f8a665710e1 to your computer and use it in GitHub Desktop.
Save arpitjain03/c876e8fd3a7707f2b1b00f8a665710e1 to your computer and use it in GitHub Desktop.
//
// PickerView.swift
// PickerView
//
// Created by Arpit on 08/01/19.
// Copyright © 2018 Arpit. All rights reserved.
//
import UIKit
class PickerView : NSObject {
// MARK: - Properties
static let shared = PickerView()
private var objPickerView : UIPickerView?
private var pickerDataSource : [String]?
private var objPickerBlock : ((_ selectedIndex:Int, _ selectedData: String) ->Void)?
private var selectedIndex :Int = 0
private var objViewController = UIViewController()
private let toolbarBtnFont = UIFont.boldSystemFont(ofSize: 17.0)
private let toolbarColor = UIColor.gray
private let toolbarBtnColor = UIColor.white
// MARK: -
/// Use this method to display **Picker View** on any textfield
///
/// - Parameters:
/// - textField: Object of textfield on which you need to an display Picker View.
/// - controller: Object of controller on which you need to an display Picker View.
/// - pickerArray: Pass your array of string for picker data
/// - completionBlock: You will get the call back here when user selected the data.
func addPickerView(textField:UITextField,
controller:UIViewController,
pickerArray:[String],
completionBlock:@escaping ((_ selectedIndex:Int,_ selectedData: String) -> Void)) {
objViewController = controller
objPickerView = UIPickerView()
objPickerView?.backgroundColor = UIColor.white
objPickerView?.delegate = self
objPickerView?.dataSource = self
pickerDataSource = pickerArray
textField.tintColor = UIColor.clear
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: controller.view.bounds.size.width, height: 44))
toolBar.barTintColor = toolbarColor
toolBar.tintColor = toolbarBtnColor
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let done = UIBarButtonItem.init(title:"Done", style: .done, target: self, action: #selector(doneClick))
done.setTitleTextAttributes([NSAttributedString.Key.font: toolbarBtnFont], for: .normal)
done.setTitleTextAttributes([NSAttributedString.Key.font: toolbarBtnFont], for: .selected)
let cancel = UIBarButtonItem(title:"Cancel", style: .done, target: self, action: #selector(cancelClick))
cancel.setTitleTextAttributes([NSAttributedString.Key.font: toolbarBtnFont], for: .normal)
cancel.setTitleTextAttributes([NSAttributedString.Key.font: toolbarBtnFont], for: .selected)
toolBar.setItems([cancel,flexSpace,done], animated: true)
textField.inputAccessoryView = toolBar
if let index = pickerArray.index(of: textField.text!) {
objPickerView!.selectRow(index, inComponent: 0, animated: true)
}
textField.inputView = objPickerView
objPickerBlock = completionBlock
}
// MARK: - Button Click Event
@objc func doneClick() {
objPickerBlock!(selectedIndex, (pickerDataSource?[selectedIndex] ?? ""))
objViewController.view.endEditing(true)
}
@objc func cancelClick() {
objViewController.view.endEditing(true)
}
}
// MARK: - UIPickerViewDataSource
extension PickerView : UIPickerViewDataSource{
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return (pickerDataSource?.count ?? 0)
}
}
// MARK: - UIPickerViewDelegate
extension PickerView : UIPickerViewDelegate{
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerDataSource?[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
selectedIndex = row
}
}
@shanchieh
Copy link

Can't work in dark screen mode. because system background issue . How about to add following code ? (in func addPickerView...)

    if #available(iOS 13.0, *) {
        if controller.traitCollection.userInterfaceStyle == .dark {
            objPickerView?.backgroundColor = UIColor.systemBackground
        }
    }

work in dark mode is OK

@arpitjain03
Copy link
Author

Thanks for the update. I will consider the same

@ebaledent
Copy link

Hello,
Sorry to bother you, but I am a beginner in Swift and I have access to your Git while trying to find a solution to my problem (having several PickerView in my screen).

How do you use your class in a viewcontroller?

Sorry to ask such a basic question, but like I said before, I'm a big beginner in Swift.

Thank you for your patience and your response.

Eric

@akbarikeyur
Copy link

You can do one thing, add the button above the text field, and when the user click on button start text field editing like this

@IBAction func clickToSelectFilter(_ sender: UIButton) { self.view.endEditing(true) let arrTemp = ARRAY.REGION PickerManager.shared.addPickerView(textField: regionTxt, controller: self, pickerArray: arrTemp) { (index, value) in self.regionTxt.text = value } regionTxt.becomeFirstResponder() }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment