Why does UINavigationBar steal touch events?

后端 未结 12 2526
耶瑟儿~
耶瑟儿~ 2020-11-27 18:48

I have a custom UIButton with UILabel added as subview. Button perform given selector only when I touch it about 15points lower of top bound. And when I tap above that area

12条回答
  •  一整个雨季
    2020-11-27 19:02

    I noticed that if you set userInteractionEnabled to OFF, the NavigationBar doesn't "steal" the touches anymore.

    So 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.

提交回复
热议问题