How To Implement Custom Dropdown List In Swift?

Expert iPhone app developers from India are sharing this tutorial with IOS developer’s community. They have intended this post to explain the bets implementation method to the novice. Through this post, other developers can learn how to implement a custom dropdown list in Swift.

One-liner: In this tutorial, we will learn how to implement the custom Dropdown list. For this tutorial we will create small drop-down with color names and selected color will be applied to application background view.

Basic Requirement:

  • Xcode 7+
  • Swift 2.0
  • iOS 8+

Start by creating one SingleViewApplication with product name SwiftDropDown. In the language field select swift and devices field select universal.

To design user interface open Main.storyboard file. Add one UIButton on top of the main view to display selected color. Create a required outlet for UIButton to ViewController.swift.

Drag one UITableView with the same width of button and align it exactly at bottom of UIButton. Create the required outlet for UITableView to ViewController.swift and also set delegate and dataSource. Please check below screenshot.

Click on prototype cell and set identifier as Cell. Please check below screenshot.

Now open ViewController.swift file and declare one NSMutableArray with some color names like Red, Yellow, Green, etc. In viewDidLoad() method set border of table view and button. At launchtimetableview will hide, all code shown in below screenshot.

Next method is the button click event method. In this method, we handle table view hide and show. Check below screenshot.

Next, are table view delegate and dataSource methods.

numberOfRowsInSection (): Set array count.

cellForRowAtIndexPath (): Set color names from array.

didSelectRowAtIndexPath (): In this method, we change button title to the selected color Name and change the view background color using a select row and also hide table view. Please check below screenshot for more info.

Now build and run a project. If everything is correct, your app should run properly, and you will see below output. You can check the dropdown list by clicking on the button and select one from the list.

This article was intended by iPhone app developers India with a purpose. They wanted to tell how a novice developer can implement custom dropdown list in swift. If there is any query, you can straightaway ask from experts through comments.

You might like

About the Author: Vijay Aegis

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.