I have a custom UIPickerView
where I use:
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
to populate the picker with UILabels
. Is there a way to disable the behavior of highlighting the selected row when touched?
I think this is a property of the underlying UITableViewCell
inherent in the UIPickerView
and I can't find a way to change it.
I'm not sure if there is an easy way to remove the selection feedback, but you can cover it up if you make the background of the label white and size it to the same dimensions as the blue selection rectangle:
With a width of 316 the label covers all but a sliver of blue on each side, and at 320 it completely covers the selection feedback but it also starts to cover a bit of the outer wheel gradients, which may or may not bother you.
Setting the
userInteractionEnabled
property ofUILabel
toYES
fixes the highlighting issue, but it also disables theUIPickerView
from autoscrolling to select the row that has been touched.If you want to disable the highlighting behavior, but maintain the
UIPickerView
's default autoscrolling functionality, call thesetShowSelection
function in theUITableCell
instances contained in theUIPickerView
. A way of doing this is to subclass theUILabel
class similar to the following:PickerViewLabel.h -
PickerViewLabel.m -
Then, where you had previously been returning an instance of
UILabel
inpickerView:viewForRow:forComponent:reusingView:
, return an instance ofPickerViewLabel
. As an example, using the code from Doug, you would replace all the cases of 'UILabel
' with 'PickerViewLabel
'. Just remember to remove thepickerRowLabel.userInteractionEnabled = YES;
line.You need to make sure your custom view has the following properties:
pickerView:rowHeightForComponent:
andpickerView:widthForComponent:
. The default height is 44 if you're not specifying a custom height.[UIColor clearColor]
.The one gotcha when using
UILabel
instances as the custom view is thatUILabel
defaultsuserInteractionEnabled
toNO
(UIView
, on the other hand, defaults this property toYES
).Based on these requirements, the example code from Halle can be rewritten as follows. This example also correctly reuses previously created views, which is needed for fast scrolling performance.
I guess you may want to look at "showsSelectionIndicator" property of UIPickerView