Search code examples
iosswiftuipickerview

UIPickerView does not display images correctly


I am implementing a custom picker view only with images. The problem is, the images are overlayed over each other like that: enter image description here

This is the code configuring the images in the picker view:

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
    let chosenImage: UIImageView = UIImageView(image: UIImage(named: iconArray[row]))
    NSLog("choosen image \(iconArray[row])")
    let workaroundImageView: UIImageView = UIImageView(frame: chosenImage.frame)
    workaroundImageView.backgroundColor = UIColor(patternImage: chosenImage.image!)
    return workaroundImageView
}

What am I missing? Any suggestions?


Solution

  • Utilize the rowHeightForComponent in the delegate for the UIPickerView.

    pickerView:rowHeightForComponent:
    

    Give the desired row height for component as such.

    func pickerView(pickerView: UIPickerView!, rowHeightForComponent component: Int) -> CGFloat