相当老的问题,但也许一个解决方案对其他人也有帮助......
我创建了一个UINavigationBar 子类,它只覆盖一个方法:' hitTest:withEvent: '。当调用hitTest:withEvent时,它会检查事件是否发生在导航栏的框架内 (pointInside:withEvent:)。如果事件发生在外部,则userInteractionEnabled标志设置为NO,因此导航栏及其子视图将忽略该事件。
就我而言,导航栏子类是通过 IB 插入的,但当然也可以通过 'UINavigationController initWithNavigationBarClass:toolbarClass:' 插入它
标题:
@interface MMMasterNavigationBar : UINavigationBar
@end
执行:
@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