UINavigationBar UIBarButtonItems much larger click

2020-02-02 11:22发布

hopefully someone can help me out- iv'e scoured the net for the answer and cannot find one-

the UIBarButtonItems added to UINavigationBar have a much larger click area than required- for example, open up any project you have a nav bar with buttons on- click anywhere between the end of the button and the title of the nav bar- the button clicks, when you clearly did not click on the button-

also try this- click underneath the nav bar, below the button, the button clicks for about 5+ pixels below the nav bar-

my problem is this-

i have added a custom header with buttons to a tableview- but when i click the buttons in the header, the UINavigationBar buttons trigger for those 5+ pixels instead of the buttons in the tableview header-

i did a test, and removed the buttons from UINavigationBar and what is interesting is that for the 5 pixels below the nav bar, the buttons in the header will not trigger even though there are no buttons in the nav bar-

its almost like the nav bar has reserved some 5+ pixels below itself as click space-

my question is this-

can someone tell me how to make the nav bar not grab those extra 5+ pixels for its buttons?

thanks very much ;)

8条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-02 12:17

As far as I know, it is impossible to turn off. If you have other buttons on the navigation bar, those click-spaces will not collide, but if you have button directly beneath the nav bar with no space at all in between, you're out of luck. Consider a small padding in the header and its buttons as a solution.

查看更多
手持菜刀,她持情操
3楼-- · 2020-02-02 12:24

Quite old question, but maybe a solution is helpful to others too...

I've created a UINavigationBar subclass, that overrides just one method: 'hitTest:withEvent:'. When hitTest:withEvent is called, it checks wether the event has happened inside the frame of the navigation bar (pointInside:withEvent:) or not. In case, the event has happened outside, the userInteractionEnabled flag is set to NO so the event will be ignored by the navigation bar and its subviews.

In my case, the navigation bar subclass is inserted via IB, but of course is is also possible to insert it via 'UINavigationController initWithNavigationBarClass:toolbarClass:'

Header:

@interface MMMasterNavigationBar : UINavigationBar

@end

Implementation:

@implementation MMMasterNavigationBar

/*
 hitTest:withEvent:

 The hit area in for navigation bar button items is enlarged by default.
 Other objects directly below the navigation bar doesn't receive tap events.
 We avoid the default enlarging of the tappable area by disabling userInteraction
 when the real tap is outside the navigation bar.

 */
-(UIView *)hitTest:(CGPoint)pPoint
         withEvent:(UIEvent *)pEvent {
    //FLog;

    if ([self pointInside:pPoint
                withEvent:pEvent]) {
        //NSLog(@"User interaction enabled");
        self.userInteractionEnabled = YES;

    } else {
        //NSLog(@"User interaction disabled");
        self.userInteractionEnabled = NO;
    }

    return [super hitTest:pPoint
                withEvent:pEvent];
}

@end
查看更多
登录 后发表回答