meaning-matters
meaning-matters

Reputation: 22986

Horizontally shifted hidden rows in UIPickerView with 2 components

I have a UIPickerView with 2 components:

enter image description here

The hidden rows however are shifted towards the center. I think it has to do with the space that UIPickerView places between the 'wheels'.

I don't see anything in iOS' rather brief documentation discussing how to prevent this.

Here's my code:

func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat
{
    return self.currenciesPicker.width / 2
}

func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
{
    return 50.0
}

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

    if view == nil
    {
        let width  = self.pickerView(pickerView, widthForComponent: component)
        let height = self.pickerView(pickerView, rowHeightForComponent: component)
        cell = CurrencyPickerCell(frame: CGRect(x: 0.0, y: 0.0, width: width, height: height))
    }
    else
    {
        cell = view as! CurrencyPickerCell
    }

    let code = self.codes[row]
    cell.nameLabel.text   = Currency.names[code]
    cell.symbolLabel.text = Currency.symbols[code]

    return cell
}

When I let widthForComponent return half the picker's size minus 20, the cells just show up 20 point smaller. This creates empty borders on the left and right ends of the picker view. It does not change this weird 'parallax'.

Upvotes: 0

Views: 313

Answers (1)

Upholder Of Truth
Upholder Of Truth

Reputation: 4711

Unfortunately this is how the UIPickerView control is designed. It's supposed to look like a couple of wheels sitting side by side that you are looking at from directly above. It's a bit of a strange one considering all the push towards a flat design style Apple has made but there is no way to change it.

The only real options are to do it yourself, either completely manually or possibly subclassing UIPickerView and handling the drawing yourself, or to use a 3rd party control.

Upvotes: 1

Related Questions