简体   繁体   English

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

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

I made it this far and it replies my text without a problem: 我到此为止,它可以毫无问题地回复我的文字:

    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() {

    }

}

And it comes to the part where I need to add a single icon to one side of each food's name. 涉及到我需要在每种食品名称的一侧添加一个图标的部分。 I can't quite figure out how it's done, but I know you can use 2 simple ways of: 我不太清楚它是如何完成的,但是我知道您可以使用以下两种简单方法:

  1. Using an extra UIView and 使用额外的UIView和
  2. Using an UIImage. 使用UIImage。

Any alternative methods are also welcome. 也欢迎任何其他方法。

You are the question and you are the answer Yes!! 您是问题,您是答案是!! here two simple way to add your idea 1)using an extra UIView and 2)using an UIImage. 这是两种添加想法的简单方法:1)使用额外的UIView和2)使用UIImage。 3)create a card And finally it's one here it's card. 3)创建一张卡片,最后是一张卡片。 but it's same as UIView 但是和UIView一样

https://github.com/aclissold/CardView https://github.com/aclissold/CardView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM