UITapGestureRecognizer and UIBarButtonItem

By July 24, 2012Blog

When you have problems with UIBarButtonItem receiving tap events when the main View has UITapGestureRecognizer, it might be because you have UIToolbar and the actual UITapGestureRecognizer added to the same View. Then the recognizer receives all the tap events even those on the UIBarButtonItems.

Ensure you handle the ShouldReceiveTouch accordingly and do not return true if the tap is on the UIBarButtonItem. This is how I handle it:

 

public class TapGestureRecognizerDelegate : UIGestureRecognizerDelegate
{
	public UITouch ActiveTouch { get; set; }
 
	public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch)
	{
		if (touch.View.Superview is UIToolbar)
			return false;
 
		ActiveTouch = touch;
 
		return true;
	}
}

 

Happy coding!