Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a drop down menu in Swift 3

How to implement the Drop Down menu in iOS using swift 3 as shown in the following image: enter image description here

I have searched SO questions but they prefer making use of UIPicker but instead i wish to achieve this . Is it possible to achieve this type by using the table view?

And that to i need to select the dates from the drop down menu:

enter image description here

How to show the dates in table view as shown below?

like image 357
Jessica thompson Avatar asked Aug 31 '25 00:08

Jessica thompson


2 Answers

There are bunch of Demo and sample for dropDown , You can achieve this by using tableView just tableview when user click of the button. or you can use this https://cocoapods.org/pods/DropDown

let dropDown = DropDown()

// The view to which the drop down will appear on
dropDown.anchorView = view // UIView or UIBarButtonItem

// The list of items to display. Can be changed dynamically
dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

Optional properties:

// Action triggered on selection
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
  print("Selected item: \(item) at index: \(index)")
}

// Will set a custom width instead of the anchor view width
dropDownLeft.width = 200
Display actions:

dropDown.show()
dropDown.hide()
like image 110
Optimus Avatar answered Sep 02 '25 15:09

Optimus


(Swift 3) Add text box and uipickerview to the storyboard then add delegate and data source to uipickerview and add delegate to textbox. Follow video for assistance https://youtu.be/SfjZwgxlwcc

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {


@IBOutlet weak var textBox: UITextField!
@IBOutlet weak var dropDown: UIPickerView!


var list = ["1", "2", "3"]

override func viewDidLoad() {
    super.viewDidLoad()
    // 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.
}


public func numberOfComponents(in pickerView: UIPickerView) -> Int{
    return 1

}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{

    return list.count

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    self.view.endEditing(true)
    return list[row]

}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    self.textBox.text = self.list[row]
    self.dropDown.isHidden = true

}

func textFieldDidBeginEditing(_ textField: UITextField) {

    if textField == self.textBox {
        self.dropDown.isHidden = false
        //if you dont want the users to se the keyboard type:

        textField.endEditing(true)
    }

}
}
like image 36
Charles Xavier Avatar answered Sep 02 '25 16:09

Charles Xavier