UIDropDownTextField is a custom class that written to draw / design an awesome Data Picker Controller with simple and easy approach. Developer can implement new Drop Down TextField with simple few steps and customize it with plenty of attributes using attributes inspector such as background, border color, separator color, font and font color etc. Class is based on Swift language and require UIKit framework for function the functionalities of the class.
To use UIDropDownTextField in your project, please follow following steps
-
Add (or Drag and drop to Your project in Xcode) UIDropDownTextField.swift files to your project
-
Add UIDropDownTextFieldDelegate to your ViewController class
Ex:
class ViewController: UIViewController, UIDropDownTextFieldDelegate
-
Declaring and initializing a IBOutlet UIDropDownTextField so an UITextField can hook from interface
Ex:
@IBOutlet var dropTextField: UIDropDownTextField!
-
Declaring and initializing an empty UIDropDownObject array so later can set drop down items from UIDropDownTextFieldDelegate datasource
Ex:
var socialMediaList: [UIDropDownObject] = [UIDropDownObject]()
-
Assign UIDropDownTextField class to your UITextField from Identity Inspector class -> Custom Class section in XCode
-
Assign UIDropDownTextFieldDelegate to UIDropDownTextField instance at your ViewController class
Ex:
override func viewDidLoad() { dropTextField.dropDownDelegate = self }
-
Add UIDropDownTextFieldDelegate datasource functions into your ViewController calss so you can catch set menu datasource to your UIDropDownTextField
Ex:
func dropDownTextField(_ dropDownTextField: UIDropDownTextField, didSelectRowAt indexPath: IndexPath) { let aDropDownObject: UIDropDownObject = socialMediaList[indexPath.row]; print("Great! You just select \(aDropDownObject.title) from UIDropDownTextField.") // Or print("Great! You can get selected text of \(dropTextField.text()) from UIDropDownTextField.") print("Great! You can get selected value of \(dropTextField.value()) from UIDropDownTextField.") } // Mark - Initializing drop down items in UIDropDownTextField datasource func dropDownTextField(_ dropDownTextField: UIDropDownTextField, setOfItemsInDropDownMenu items: [UIDropDownObject]) -> [UIDropDownObject] { return socialMediaList }
-
Initializing a new UIDropDownObject with object using previous UIDropDownObject variable
Ex:
override func viewDidLoad() { let facebook = UIDropDownObject(title: "Facebook", value: "Facebook", icon: UIImage.init(named: "ic_facebook")) let linkedin = UIDropDownObject(title: "Linked in", value: "Linked in", icon: UIImage.init(named: "ic_linkedin")) socialMediaList = [facebook,linkedin] }
-
Use IBInspectable properties to customize your awesome UIDropDownTextField at Attribute Inspector panel in XCode
Created by Nadeeshan Jayawardana (NEngineering).