Allowing user to select a UIPickerView row by tapping
Solution 1
Add a gesture recogniser to the UIPickerView which triggers a target method in your object:
myGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerTapped:)];
[myPicker addGestureRecognizer:myGR];
// target method
-(void)pickerTapped:(id)sender
{
// your code
}
Solution 2
make a new UIControl
- same position as the UIPickerView
[yourcontrol setBackGroundColor: [UIColor clearColor]];
make a method
- (IBAction) pickerControlTapped { [yourpicker selectRow: rand()% yourpickersize inComponent: 0 animated: YES]; }
.3. make a connection between 1 and 2
[yourcontrol addTarget: self action: @selector(pickerControlTapped) forControlEvents: UIControlEventTouchUpInsied];
Solution 3
Building on Martin Linklater's answer to support tapping on the pickers other rows: Has some magic numbers but works for me.
- (void) pickerTapped:(UITapGestureRecognizer*)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.pickerView];
CGFloat halfViewHeight = self.pickerView.frame.size.height / 2;
NSInteger row = -1;
if (location.y < halfViewHeight - 22
&& location.y > halfViewHeight - 66)
{
row = [self.pickerView selectedRowInComponent:0] - 1;
}
else if (location.y < halfViewHeight + 22
&& location.y > halfViewHeight - 22)
{
row = [self.pickerView selectedRowInComponent:0];
}
else if (location.y < halfViewHeight + 66
&& location.y > halfViewHeight + 22)
{
row = [self.pickerView selectedRowInComponent:0] + 1;
}
if (row >= 0 && row < [self.content count])
{
id element = [self.content objectAtIndex:row];
if (element)
{
[self.pickerView selectRow:row inComponent:0 animated:YES];
// do more stuff
}
}
}
Solution 4
I have a relatively simple solution to this problem that has worked well for me. Using a hidden custom button you can achieve the tap functionality without a gesture recogniser. This solution works for a picker with one component, however I'm sure it could be adapted to work with more.
Firstly add a button, either in the Interface Builder or programatically. Make it hidden and as wide as the picker then place it so that it sits exactly in the centre of the picker and also in front of it in the view hierarchy.
I'm using an IBAction like this to show my picker. However it's really up to you how you show and hide the picker.
- (IBAction)showPicker:(id)sender
{
_picker.hidden = NO;
_buttonPicker.hidden = NO;
}
All the action for choosing the picker value happens in an IBAction for the UIControlEventTouchUpInside event, something like this.
- (IBAction)selectPicker:(id)sender
{
//Hide the button so that it doesn't get in the way
_buttonPicker.hidden = YES;
//Make sure we're within range
NSInteger max = _values.count;
NSInteger row = [_picker selectedRowInComponent:0];
if(row >= 0 && row < max) {
NSString *value = [_values objectAtIndex:row];
//Set the label value and hide the picker
_label.text = value;
_picker.hidden = YES;
}
}
I've slightly modified the code for this answer from working code so apologies if it's broken at all.
camilo
Updated on June 14, 2022Comments
-
camilo almost 2 years
I'm trying to use a UIPicker View with a behavior somehow different of what's usually seen in iPhone code examples.
What I want to do is to allow users to scroll through the picker contents, but not to select a picker's row automatically (using the "didSelectRow" method from picker's delegate). Instead, I want to allow the user to touch the center row of the picker, which gets highlighted, and becomes the selection.
Is there any way to achieve this?
Thanks in advance.
-
camilo about 14 yearsthat's what I was thinking. I've seen applications doing what I want, but I suppose i'm doing it some other way. Thanks anyway.
-
Jared Thirsk over 12 yearsExcellent, please mark this as answer. Note: make it the same position as the center row of the UIPickerView.
-
Dean Davids over 12 yearsMartin, I happened upon your solution and found it very helpful. So simple and it works. The only issue, in my response to the tap, how can I pass the tap through to the picker so that I get the object at the line that is tapped, not the object at center?
-
Grzegorz D. about 11 years@DeanDavids It seems the page you've pointed doesn't exist any more. Could you provide another one, or explain the solution?
-
Roddy about 10 yearsI think this is simple and brilliant. Since the selecting row height is already known, it can be positioned precisely and simply. Great stuff, thanks.
-
Roddy about 10 yearsSorry, I just have to say again how excellent this solution is, has made my afternoon.
-
jp36 over 6 yearsTo any googlers who find this answer, there is another stackoverflow answers that handles allowing a click to passthrough and select an item - stackoverflow.com/a/25719326/655822
-
jp36 over 6 yearsTo any googlers who find this answer, there is another stackoverflow answer that handles allowing a click to passthrough and select an item without having to rely on magic numbers - stackoverflow.com/a/25719326/655822