Custom Drop Down | Swift 4
Implement the Drop Down menu in iOS using swift 4 in xcode 9. I have searched questions Custom Drop Down, but they prefer making use of UIPicker . Is it possible to achieve this type by using the table view, so finaly i use found this way.
1. Create layout like this
2. in UIviewController.swift
import UIKit
class DropDownViewController: UIViewController {
@IBOutlet var textField: UITextField!
@IBOutlet var bV: UIButton!
@IBOutlet var tabelView1: UITableView!
@IBOutlet var textFieldEdit: UITextField!
//keyboard (bottom uitextfield)
@IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?
let array = ["data1", "data2", "data3", "data4", "data5"]
@IBAction func bActionV(_ sender: Any) {
self.tabelView1.isHidden = !self.tabelView1.isHidden
}
override func viewDidLoad() {
super.viewDidLoad()
self.tabelView1.isHidden = true
self.tabelView1.register(UINib(nibName: "ApiTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
//keyboard
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
//keyboard
deinit {
NotificationCenter.default.removeObserver(self)
}
//keyboard
@objc func keyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let endFrameY = endFrame?.origin.y ?? 0
let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
if endFrameY >= UIScreen.main.bounds.size.height {
self.keyboardHeightLayoutConstraint?.constant = 0.0
} else {
self.keyboardHeightLayoutConstraint?.constant = (endFrame?.size.height)! + 50
}
UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: { self.view.layoutIfNeeded() }, completion: nil)
}
}
//---------------------
}
extension DropDownViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 10
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ApiTableViewCell
cell.merkLabel.text = array[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as? ApiTableViewCell
self.textField.text = cell?.merkLabel.text
bV.setTitle(cell?.textLabel?.text, for: .normal)
self.tabelView1.isHidden = true
}
}
or try this video
No comments: