Create a drop down utilizing swift language in iOS – iOSTutorialJunction


iOS tutorial: Create a drop down utilizing swift language in iOS

On this tutorial, we are going to discover ways to create a drop down utilizing swift language in iOS app growth. For creating dropdown we are going to use a cocoa-pod named “DropDown” by assistolab. Given beneath is the url for the pod documentation, you may verify that for higher understanding of the drop-down performance

Putting in Dopdown pod utilizing terminal

Step1: Open terminal, and set your listing the place your undertaking is saved, verify picture proven beneath. Be aware that path of listing shall be until folder that accommodates .xcodeproj file.

Setting directory path using CD command in terminal to install pod
Setting listing path to put in pods

Step 2: Run beneath instructions to initialize pod for the undertaking and podfile
pod init
open -e podfile

In poddile add pod identify as instructed in documentation of the dropdown pod, and eventually in terminal run
pod set up

Step 3: At this level we had, pod efficiently added to our undertaking. Open undertaking by clicking file named ”.xcworkspace”

Step 4: In file ViewController.swift, import module DropDown and add beneath line of codes

@IBOutlet weak var vwDropDown:UIView!
@IBOutlet weak var lblTitle:UILabel!    
let dropDown = DropDown()    
let fruitsArray = ["mango", "apple", "banana", "cherry"]

In above code snippets, we created two IBOutlets one is of UIView kind and second one is of UILabel kind. Create an occasion of DropDown module put in utilizing pods, and an array holding values we’re going to present as dropdown choices.
In our important.storyboard we already designed person interface as proven beneath(as we aren’t overlaying the design half on this weblog submit)

User interface for this tutorial

Configuring dropdown with datasource and its anchor factors

In our viewcontroller’s viewDidLoad operate, we are going to configuring datasource for dropdown choices. Additionally we are going to set anchor level fro dropdown, and write dropdown’s callback methodology that gave us the choice chosen by person. Beneath is the code for dropdown configurations and registering choice callback

    override func viewDidLoad() {
        // Do any further setup after loading the view.
        lblTitle.textual content = "Choose a fruit"
        dropDown.anchorView = vwDropDown
        dropDown.dataSource = fruitsArray
        dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.peak)!)
        dropDown.course = .backside
        dropDown.selectionAction = { [unowned self] (index: Int, merchandise: String) in
          print("Chosen merchandise: (merchandise) at index: (index)")
            self.lblTitle.textual content = fruitsArray[index]

In above code, first we set textual content for UILabel identify lblTitle as default placeholder string. In subsequent line we inform compiler that, vwDropdown shall be our anchorview for the dropdown. Subsequent, we assign fruits array as a datasource. We have to inform concerning the backside offset for our dropdown. Backside offset will inform dropdown that make its y offset as the peak of anchorview. Inform the course for opening of dropdown. Lastly, we write down the callback for our dropdown choice.

Current dropdown to person’s

In an effort to present dropdown we are going to write, IBAction for our button and name beneath line of code.

The place to go from right here

On this submit we discovered, create a dropdown in iOS app growth utilizing swift language. For creating dropdown, we used assistolab pod. I hope this submit, helped you in studying create dropdown in iOS utilizing swift language.