How to change color of selected row in UIPickerView

前端 未结 10 928
孤独总比滥情好
孤独总比滥情好 2020-12-03 10:25

Ok, maybe I\'m missing something really simple and I apologize if that\'s the case, however, I\'ve googled every permutation of the title and have not found! So this is sim

10条回答
  •  鱼传尺愫
    2020-12-03 11:27

    Swift version of @alessandro-pirovano answer

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    
        let rowSize = pickerView.rowSize(forComponent: component)
        let width = rowSize.width
        let height = rowSize.height
        let frame = CGRect(x: 0, y: 0, width: width, height: height)
        let label = UILabel(frame: frame)
        label.textAlignment = .center
        label.text = rows[row]
    
        return label
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
        guard let label = pickerView.view(forRow: row, forComponent: component) as? UILabel else {
            return
        }
        label.backgroundColor = .orange
    }
    

提交回复
热议问题