Search code examples
iosuipickerviewandroid-spinner

How to have an android like spinner view in iOS?


enter image description here My requirement is to show the users all the selection options to the user so that he can see all at once an then able to select from those just like it is done with spinner view in android. Currently I am using UI picker view. I am attaching the screen shot of it. Either I want a spinner or want the picker view to show 10 items simultaneously for the same height where it is currently showing three. This is my code:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
            var myString = view as! UILabel!

            var myStringLength = 0

            if( view == nil) {
                myString = UILabel()
            }
            myString?.textAlignment = .center

            var rowText:String

            if(pickerView==District){
                rowText=districts[row]
            }
            else if(pickerView==block)
            {
                 rowText=blocks[row]
            }
            else if(pickerView==management)
            {
               rowText=managements[row]
            }
            else
            {
                 rowText=categories[row]
            }

            var attributedRowText = NSMutableAttributedString(string: rowText)
            var attributedRowTextLength = attributedRowText.length

            attributedRowText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location: 0, length: attributedRowTextLength))

            attributedRowText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 8.0)!, range: NSRange(location: 0 ,length:attributedRowTextLength))

            myString!.attributedText = attributedRowText

            return myString!
        }

Solution

  • You can use table view as a dropdown:

    subCategoryTable is table name. Create this table on press of that dropdown button. here subCatgBtn is that btn on click write this code.

        subCategoryTable.frame         =   CGRectMake(self.subCatgBtn.frame.origin.x, self.subCatgBtn.frame.origin.y + self.subCatgBtn.frame.size.height, self.subCatgBtn.frame.size.width,CGFloat(numberofrows * 2));
        subCategoryTable.delegate      =   self
        subCategoryTable.dataSource    =   self
        subCategoryTable.tag = 2
        //subCategoryTable.rowHeight = self.subCatgBtn.frame.size.height
        subCategoryTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")//this is needed when you use custom cell otherwise no need
        subCategoryTable.separatorColor = UIColor.lightGrayColor()
        subCategoryTable.allowsSelection   = true
        self.view.addSubview(subCategoryTable)
    

    CellforrowIndex:

         func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        //        var cell = tableView.dequeueReusableCellWithIdentifier("cell")!
        if tableView.tag == 2
        {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
    
             //do whatever you want to show.
         }
       }
    

    numberofrows method:

       func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 2
        {
    
         districts.count
        }
        }
    

    and on Didselect method just set the name on that button whatever you selected.

    I dont know Your UI how you designed, You can use this one as dropdown it will create dynamically when you click on that button.