Search code examples
swiftuipickerview

UIPicker selection appears white


I am having some trouble implementing the UIPicker. When I view it, some rows appear white/transparent. After scrolling a bit and coming back the row finally appear.

Here is the screenshot:

1st row staying white

enter image description here

2nd row as well while scrolling on it

enter image description here

Rows finally appear after fully scrolling and returning on them

enter image description here

Here is the code

// reinitializing array to nil
pickerContentArray = []

for i in 0..<specialitiesList.count {
    pickerContentArray.append(specialitiesList[i]["name"] as! String)
}

picker.reloadAllComponents()
picker.selectRow(0, inComponent: 0, animated: false)

    //MARK: - Delegates and data sources
//MARK: Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerContentArray.count
}

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
    var pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blackColor()

    if pushedButton == "Speciality" {
        pickerLabel.text = specialitiesList[row]["name"] as? String
        pickerLabel.font = UIFont(name: "Helvetica", size: 12)
    } else if pushedButton == "Status" {
        pickerLabel.text = statusList[row]
        pickerLabel.font = UIFont(name: "Helvetica", size: 16)
    } else if pushedButton == "City" {
        pickerLabel.text = citiesList[row]["name"] as? String
        pickerLabel.font = UIFont(name: "Helvetica", size: 16)
    }

    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
}

//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return pickerContentArray[row]
}

Thank you,


Solution

  • Perhaps you are calling your setup code too early.

    Try to do it in viewWillAppear() instead of viewDidLoad().

    Also, it could be that because you select the component without animation, the component does not update properly. You should pass false for animation before the view is shown, but true if the picker is already visible to the user.