设置UIPickerView行的背景颜色

时间:2019-10-11 08:51:12

标签: ios swift

我有两个关于选择器视图的问题。

如何为特定行设置pickerview背景颜色?我正在尝试制作具有另一种背景颜色的“部分”。另外,如何确保它们不可滚动?我的意思是,用户应该无法“停止”该选择,该应用程序应向下滚动到下一个可用的选项。

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

    if WhatBool == true{

    }
}

我目前有这一行。当我在这个问题上搜寻时,找不到任何实际有效的内容,也找不到特定的行。我做错了吗?如何在pickerview中创建节?一切似乎都已过时,因此我很乐意为此提供一些意见。

我想我应该在viewForRow中更改此类内容。但是我现在完全被困住了。

1 个答案:

答案 0 :(得分:2)

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

        var label:UILabel

        if let v = view as? UILabel{
            label = v
        }
        else{
            label = UILabel()
        }
        label.backgroundColor = component == 0 ? .red : .purple
        label.textColor = UIColor.white
        label.textAlignment = .left
        label.font = UIFont(name: "Helvetica", size: 16)

        label.text = arrayOfCountries[row]

        return label
    }

针对不同的背景,请尝试使用此逻辑,并且对于禁用特定组件,请遵循以下答案

Swift UIPickerView disable component