Search code examples
iosuinavigationcontrolleruibuttonuinavigationbaruinavigationitem

Navigation Bar Navigation Items causing top area of buttons on pushed view controller to not be tappable


I have a strong feeling that this might be a bug with Xcode that apple just needs to fix. I have a view controller that is embedded in a navigation controller as the root controller. The view controller has a button that is vertically aligned with the top layout guide. The view controller's button works fine and all of the tappable area is working.

enter image description here

However, if I push the same or a similar view controller on to the navigation stack, it's button will not be completely tappable. The top of the button (about 10 pixels or so) is no longer tappable. If I try to tap at the top left of the button, the back button on the navigation bar is tapped instead, even though I am clearly not tapping the navigation bar bounds. I assume this is a bug with apple but I was wondering if anyone knows of a fix. Here is the link to the github project if anyone needs it.


Solution

  • This is the default behavior for iOS. Many UIViews have this extended touch functionality in iOS. For example, UINavigationBar, UITabBar, UISecgmentedControl etc. I believe this is to make easer touch to these controls.

    If you still want to override this default behavior. You can do this by subclassing UINavigationBar and add this method in the subclass:

    -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
        if ([self pointInside:point withEvent:event]) {
            self.userInteractionEnabled = YES;
        } else {
            self.userInteractionEnabled = NO;
        }
    
        return [super hitTest:point withEvent:event];
    }
    

    A merge request to your Github project is made.