iOS 7.1 UITapGesture not working with UIPickerView

Anupam picture Anupam · Mar 11, 2014 · Viewed 11.2k times · Source

We are using a UIPickerView to allow a user to select from a list of options. We are adding UIPickerView as a subview of a container UIView. We are then adding a UITapGestureRecognizer to the container UIView. The UITapGestureRecognizer is being used to dismiss the picker via removing it's super view.

In iOS 7.0 and previous versions, this was working as expected. However, in iOS 7.1 this set up is no longer working in the sense that UITapGestureRecognizer is not recognizing the tap and calling the selector specified in the action (dismissing the picker view and container view). code is below

   - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.nameList=[[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C", nil];
    UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    myPickerView.delegate = self;
    myPickerView.showsSelectionIndicator = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapInListPickerView:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [myPickerView addGestureRecognizer:singleTap];
    [self.view addSubview:myPickerView];
}

-(void)tapInListPickerView:(UIGestureRecognizer *)sender

{
    NSLog(@"Taped in pikcer view");
}

If any other info is needed or if there is a more preferred method for doing this, please let me know.

Answer

ZDidier picture ZDidier · Mar 15, 2014

I had the same problem, and I finally had a revelation :P

It was like simultaneous gesture rencognizers on uiPickerView don't work.

so I use the gesture delegate

<
UIGestureRecognizerDelegate>

with

 // add tap gesture
    UITapGestureRecognizer* gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerViewTapGestureRecognized:)];
    [picker addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.delegate = self;

with

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    // return
    return true;
}

and then its working!

See You