Swift 4 如何在 UIPickerView 中的文本旁边添加图像或图标?

How to add images or icons next to a text in UIPickerView in Swift 4?

我做到了这一点,它毫无问题地回复了我的文字:

    class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var foodLabel: UILabel!
    @IBOutlet weak var foodSlider: UIPickerView!

    var picker = UIPickerView()

    let foodsScroll = ["pizza \npepperoni" ,"chicken \nnuggets ","meat \nballs","hamburger \nbloody","omelette \ngrilled","ice cream \nberry"]

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 80.00
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        let cgRect = CGRect(x: 0.00, y: 0.00, width: 400, height: 200)
        let label = UILabel(frame: cgRect)
        label.lineBreakMode = .byWordWrapping
        label.numberOfLines = 0
        label.text = foodsScroll[row]
        label.sizeToFit()
        return label
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return foodsScroll[row]
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return foodsScroll.count
    }


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        foodLabel.text = foodsScroll[row]
    }

    override func viewDidLoad() {

    }

}

到了我需要在每种食物名称的一侧添加一个图标的部分。我不太明白它是如何完成的,但我知道你可以使用两种简单的方法:

  1. 使用额外的 UIView 和
  2. 使用 UIImage。

也欢迎任何替代方法。

你就是问题,你就是答案是的!!这里有两种简单的方法来添加你的想法 1)使用额外的 UIView 和 2) 使用 UIImage。 3)创建一张卡片 最后这是一张卡片。但它与 UIView

相同

https://github.com/aclissold/CardView