Tejasvi Tandon Tejasvi Tandon - 1 month ago 16
iOS Question

UITapGestureRecognizer not recognizing taps

I have created a viewcontroller programatically and added a sub view on the upper half of the view controller. However when I try to add tap gestures to my subview it does not seem to recognise the taps.When I tap on the sub view(menuSubView) pointer does not call the -(void) tapGestureMethod. Below is my code-

UITapGestureRecognizer *tapMenuSubView=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureMethod:)];
tapMenuSubView.numberOfTapsRequired=1;
tapMenuSubView.numberOfTouchesRequired=1;
tapMenuSubView.delegate=self;
self.view.userInteractionEnabled=YES;
self.menuSubView.userInteractionEnabled=YES;
[self.menuSubView addGestureRecognizer:tapMenuSubView];
}
-(void)tapGestureMethod:(UITapGestureRecognizer *)gesture{
UIView *tapView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview: tapView];


}

Answer

From the code you sent it's hard to say, what might be a problem. We don't know, what's inside a menuSubView's class, how do you create menuSubView, etc.

Without knowing it, I would suggest to remove temporarily the tapMenuSubView.delegate=self;. It depends on your logic, but in the basic case, you don't need the delegate to make a gesture recognizer work.

If it doesn't help, I would go deeper into a setup of the menuSubView to ensure nothing intercepts taps. Maybe, there's an invisible view which covers it. Or other gesture recognizers which might cause problems.

UPDATE

The problem is in this method.

- (IBAction)menuButton:(id)sender {
    MenuViewViewController *menuViewObj=[[MenuViewViewController alloc]init];
    menuViewObj.view.frame=CGRectMake(0, 0, self.view.frame.size.width-80, self.view.frame.size.height);

    [menuViewObj.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:menuViewObj.view];
}

You don't correctly embed MenuViewViewController into ViewController. This tutorial should help.

- (void)displayContentController:(UIViewController *) content {
   [self addChildViewController:content];
   content.view.frame = [self frameForContentController];  // set any frame you like
   [self.view addSubview:self.currentClientView];
   [content didMoveToParentViewController:self];
}
Comments