Drop Down List iOS Swift 5

Almost every project there is a need to integrate a drop-down list so here we are going to demonstrate a very good cocoapods library. It is very easy to integrate and comes with a lot of features like multiple selections, shadow properties, fully customizable, custom cell, and many more.
Let’s start coding

-In your Podfile write below:
pod 'DropDown'
– Now run a command on terminal "pod install"
– Now open your ViewController and make the below changes.
– Add a UIButton in your Nib file and create a IBAction on it
import UIKit
import DropDown //1

class ViewController: UIViewController {
  
  let dropDown = DropDown() //2
  
  override func viewDidLoad() {
    super.viewDidLoad()
  }
  
  @IBAction func tapChooseMenuItem(_ sender: UIButton) {//3
    dropDown.dataSource = ["Tomato soup", "Mini burgers", "Onion rings", "Baked potato", "Salad"]//4
    dropDown.anchorView = sender //5
    dropDown.bottomOffset = CGPoint(x: 0, y: sender.frame.size.height) //6
    dropDown.show() //7
    dropDown.selectionAction = { [weak self] (index: Int, item: String) in //8
      guard let _ = self else { return }
      sender.setTitle(item, for: .normal) //9
    }
  }
}

1: Import DropDown library
2: Create an instance of DropDown Class
3: This is a button click action, on click it we will show the drop-down list
4: Assign a datasource(array of items) to dropDown
5: Assign sender (UIButton instance) to anchorView property, on this view(UIButton) dropdown will display
6: Set the bottomOffset of the drop-down otherwise it will hide the UIButton
7: Show the drop-down list
8: SelectionClosure is a closure that executes when you tap on any item on the drop-down list. You will get index and a String object in the closure
9: Set the selected item name to UIButton title
Now run the code

Adding a drop-down inside UITableView

We can even add this dropdown inside the UITableView very easily.
Write the below code to add this drop-down inside UITableView on cell tap.
import UIKit
import DropDown

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  
  @IBOutlet weak var tableView: UITableView!
  let dropDown = DropDown()
  
  override func viewDidLoad() {
    super.viewDidLoad()
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "movie \(indexPath.row+1): "
    return cell
  }
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
      dropDown.dataSource = ["Inception", "Avatar", "Joker", "Avengers: Endgame", "Interstellar", "Gravity"]
      dropDown.anchorView = cell
      dropDown.bottomOffset = CGPoint(x: 0, y: cell.frame.size.height)
      dropDown.backgroundColor = .orange
      dropDown.show()
      dropDown.selectionAction = { [weak self] (index: Int, item: String) in
        guard let _ = self else { return }
        cell.textLabel?.text = "movie \(indexPath.row+1): \(item)"
      }
    }
  }
}

There are lots of customization are available as you can change the background color, you can add a custom cell to the drop-down list item, you can set the direction, you can set the width of the drop-down list and many more for mode detail click here
Credit: https://github.com/AssistoLab/DropDown
Read our next article: Custom URL Scheme | Deep-Link iOS 13 and later Swift 5

Leave a Reply

Your email address will not be published. Required fields are marked *