Search code examples
ioseventsuinavigationitem

Navigation Item's click or touch event sometimes does not being fired


Most of the time navigation item in navigation bar works fine. Navigation bar is hidden before user taps screen.
Navigation bar moves using [[self navigationContoller] setNavigationBarHidden:YES/NO animated:YES]

However, sometimes navigation item does not fire touch up inside event.
This is snippet of one of them.
[self.button addTarget:self action:@selector(someFunc) forControlEvents:UIControlEventTouchUpInside]

Button animates fade out and in just like being touched fine, but event does not being fired.
But when I make navigation bar to hide and emerge again by tapping screen, navigation items works fine again.

What's the problem? I have totally no idea.
Thanks in advance for your help.


Solution

  • I solved this problem by using UITapGestureRecognizer and UIImageView.
    I added both default UIBarButtonItem and customized UIBarButtonItem using UIImageView and UITapGestureRecognizer to navigationBar for test. Then when I faced the problem again, the default UIBarButtonItem did not respond but customized UIBarButtonItem worked fine.

    This is my sample code.

    UIImage buttonImage = [UIImage imageNamed:@"buttonImage" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
    UIImageView buttonImageView = [[UIImageView alloc] initWithImage:buttonImage];
    UITapGestureRecognizer buttonTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(targetFunction)];
    [buttonImageView addGestureRecognizer:buttonTapGesture];
    UIBarButtonItem *bbiButton = [[UIBarButtonItem alloc] initWithCustomView:buttonImageView];