簡體   English   中英

在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM