如何只显示2行UIPickerView?

时间:2017-02-15 04:46:55

标签: swift uipickerview

实际上,我想在UIPickerView中仅显示两行数据超过2行。因此,每当用户向上/向下滚动时,用户只能显示两行。

通过以下图片了解我想要的内容:

enter image description here

我试过下面的代码

 var pickerDataSource = ["1", "2", "3", "4","5","6", "7", "8", "9","10","11","12"];
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView
{
    var label = view as! UILabel!
    if label == nil {
        label = UILabel()
    }
    let titleData : String?


    titleData = pickerDataSource[row]
    if row == pickerView.selectedRowInComponent(0)
    {
        let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName: UIFont(name:"Montserrat-Regular", size: 16.0)!,NSForegroundColorAttributeName:UIColor.lightGrayColor()])
        label.attributedText = myTitle
    }
    else
    {
        let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName: UIFont(name:"Montserrat-Regular", size: 16.0)!,NSForegroundColorAttributeName:UIColor.redColor()])
        label.attributedText = myTitle
    }
    label.textAlignment = .Center
    return label

}

这将改变行的颜色,但我只设置两行。

有没有办法实现这样的目标?

1 个答案:

答案 0 :(得分:0)

当您有许多数据时,无法在两行之间滚动。但是我实现了DLPickerView,您可以使用此选择器视图控制滚动范围。