Customizing UIPickerView (background and spacing)

前端 未结 4 1562
無奈伤痛
無奈伤痛 2020-12-31 22:07

I would like to change the white background in a UIPickerView to an image of my own.

Is this possible?

Also, I have managed to get my UIPickerView to scroll

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-31 22:50

    Hi There is no direct way to change the background. What you can do is to have the view you return in viewForRow feature its own background (then add the shadow in each side afterwards if you need it). You can also go looking for viewWithTag: but that is never a good idea as this might change in future iOS releases.

    Is there a special reason you implement both viewForRow and TitleForRow? I usually just populate the viewForRow's labels inside this delegate method.

    The viewForRow has the ability to reuse the views in the Picker, much like a UITableView you should test if the "reusingView:(UIView *)view" is nil and if not there is no need to draw everything again. Just populate the labels.

    I usually never customize the picker, if I need something not completely custom I subclass the UITableView, it is much more flexible and can what the Picker does + more.

    For the spacing you can use the "height" of the rows, the picker will center the views you return in viewForRow, then just make sure:

    - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
    

    returns a value bigger than your view.

    Held og lykke;)

提交回复
热议问题