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

时间:2017-11-06 12:21:46

标签: ios swift xcode uipickerview swift4

我做到了这一点,它回答了我的文字没有问题:

    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。
  3. 也欢迎任何替代方法。

1 个答案:

答案 0 :(得分:0)

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

https://github.com/aclissold/CardView