import UIKit
class ViewController: UIViewController {
let FilterDropDown = DropDown()
var selectedindex = 0
var previousText = “”
@IBOutlet var txtSearch: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.txtSearch.addTarget(self, action: #selector(searchFieldChanged(sender:)), for: .editingChanged)
self.txtSearch.layer.cornerRadius = 5.0
self.txtSearch.layer.masksToBounds = true
self.txtSearch.layer.borderColor = UIColor.lightGray.cgColor
self.txtSearch.layer.borderWidth = 2.0
self.txtSearch.setLeftPaddingPoints(8.0)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet var openaction: UIButton!
@IBAction func openaction(sender: Any) {
FilterDropDown.dismissMode = .onTap
FilterDropDown.direction = .any
FilterDropDown.anchorView = openaction
FilterDropDown.bottomOffset = CGPoint(x: -50, y: openaction.bounds.height)
let Filter : NSMutableArray = [“First”,“Secoend”,“Thired”]
FilterDropDown.dataSource = (Filter as? [String])!
FilterDropDown.selectionAction = { (index, item) in
print(index)
print(item)
self.selectedindex = index
}
FilterDropDown.show()
}
@objc func searchFieldChanged(sender: Any) {
let textField = sender as! UITextField
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(searchAndReload(keyWord:)), object: previousText)
let trimmedString = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmedString != “” {
self.perform(#selector(searchAndReload(keyWord:)), with: textField.text, afterDelay: 0.5)
}else {
print(“field is blank”)
}
previousText = textField.text!
}
@objc func searchAndReload(keyWord: String){
print(keyWord)
}
}
extension UITextField {
func setLeftPaddingPoints(_ amount:CGFloat){
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
self.leftView = paddingView
self.leftViewMode = .always
}
func setRightPaddingPoints(_ amount:CGFloat) {
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
self.rightView = paddingView
self.rightViewMode = .always
}
}