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’ll discover ways to create a drop down utilizing swift language in iOS app improvement. For making a dropdown, we’ll use a cocoa-pod named “DropDown” by assistolab. Given under is the url for the pod documentation. You may examine that for higher understanding in regards to the performance or options present by assistolab dropdown library.


Putting in Dopdown pod utilizing terminal

Step1: Open terminal, and set path of your listing to the trail of your challenge, examine picture proven under. Word that path of listing shall be until folder that comprises .xcodeproj file.

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

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

In poddile add pod title as instructed in documentation of the dropdown pod, and at last in terminal run
pod set up

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

Step 4: In file ViewController.swift, import module DropDown and add under 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 sort and second one is of UILabel sort. Create an occasion of DropDown module put in utilizing pods, and different of string array holding values we’re going to present as dropdown choices.
In our foremost.storyboard we already designed consumer interface as proven under(as we’re not masking 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’ll configuring datasource for dropdown choices. Additionally we’ll set anchor level for dropdown, and write dropdown’s callback technique that provides us the choice seleccted by consumer. Under 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.top)!)
        dropDown.route = .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 title 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 data-source. We have to inform in regards to the backside offset for our dropdown. Backside offset will inform dropdown that make its y offset as the peak of anchorview. Subsequent line of code, tells the route for opening of dropdown. Lastly, we write down the callback for our dropdown choice.

Current dropdown to consumer’s

So as to present dropdown we’ll create an, IBAction for our button and name under line of code.

The place to go from right here

On this submit, we realized easy methods to create a dropdown in iOS app improvement utilizing swift language. For creating dropdown, we used assistolab pod. I hope this submit, helped you in studying easy methods to create dropdown in iOS utilizing swift language.



Leave a Comment