UINavigationBar UIBarButtonItems much larger click area than required

后端 未结 8 1460
遇见更好的自我
遇见更好的自我 2020-12-08 09:40

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 a

8条回答
  •  不思量自难忘°
    2020-12-08 10:22

    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
    

提交回复
热议问题