Touch events within ~8 pixels of nav bar not called

白昼怎懂夜的黑 提交于 2019-12-03 02:58:55
Andrei Radulescu

You have to subclass your UINavigationBar and in your CustomNavigationBar do this:

-(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];
}

Info about how to subclass UINavigationBar you can find here.

The code provided by @Andrei will cause controller stack inconsistency on iOS 7 when quickly push and pop controllers. The system itself will change the userInteractionEnabled property before and after push/pop animations. Here is how I fixed this issue.

@interface DMNavigationBar ()

@property (nonatomic, assign) BOOL changingUserInteraction;
@property (nonatomic, assign) BOOL userInteractionChangedBySystem;

@end

@implementation DMNavigationBar

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

    if ([self pointInside:point withEvent:event])
    {
        self.changingUserInteraction = YES;
        self.userInteractionEnabled = YES;
        self.changingUserInteraction = NO;
    }
    else
    {
        self.changingUserInteraction = YES;
        self.userInteractionEnabled = NO;
        self.changingUserInteraction = NO;
    }

    return [super hitTest:point withEvent:event];
}

- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled
{
    if (!self.changingUserInteraction)
    {
        self.userInteractionChangedBySystem = YES;
    }
    else
    {
        self.userInteractionChangedBySystem = NO;
    }

    [super setUserInteractionEnabled:userInteractionEnabled];
}

@end

Had the same problem and the above answers are good and they point to a concise solution.

The same answer for the swift version:

  class APLVNavigationBar: UINavigationBar {
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
    if pointInside(point, withEvent: event){
        userInteractionEnabled = true
    }else{
        userInteractionEnabled = false
    }
    return super.hitTest(point, withEvent: event)
  }
}

I dont think that it is the problem, but did you bring the button subviews to the front?

[self.view bringSubviewToFront:buttonView]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!