简体   繁体   English

在iOS中无法点击导航栏外部的区域

[英]Area outside Navigation bar is not clickable in iOS

I have created custom navigation bar and added one UIButton over it. 我创建了自定义导航栏,并在其上添加了一个UIButton。

Please take a look at image attached. 请看所附图片。

在此处输入图片说明

I have UIButton with background image added as subview to Custom NavigationBar so portion above GREEN line is clickable not the portion below green line. 我将带有背景图像的UIButton作为子视图添加到“定制导航栏”,因此可以单击绿色线以上的部分,而不单击绿色线以下的部分。

I have tried using UITapGestureRecognizer and also the touches methods ie. 我试过使用UITapGestureRecognizer以及touches方法,即。 touchesBegan to UIImageView to handle the touch, but no luck. touches开始使用UIImageView来处理触摸,但是没有运气。

I think this is only because my UIButton nontouchable potion is outside of NavigationBar Frame. 我认为这仅仅是因为我的UIButton不可触摸部分在NavigationBar Frame之外。

Is there any way to click the subview's portion which is ouside its parent view. 有什么方法可以单击子视图的一部分,而不是其父视图。

The parent view size is less than the Child view . 父视图的尺寸小于子视图的尺寸。 That's why it is non clickable. 这就是为什么它不可点击。 Asper my knowledge only option you have is try to increase the parent view (Custom Navigationbar) frame size . 据我所知,您唯一的选择是尝试增加父视图(Custom Navigationbar)框架的大小。

Yo need subclass Navigation Bar and add method hitTest:withEvent: . 您需要子类Navigation Bar并添加方法hitTest:withEvent: For example: 例如:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {    
    CGRect extraTapRect = ...; // Put here needed rect
    if (CGRectContainsPoint(extraTapRect, point)) {
        for (UIView *subview in self.subviews.reverseObjectEnumerator) {
            CGPoint subviewPoint = [subview convertPoint:point fromView:self];
            UIView *result = [subview hitTest:subviewPoint withEvent:event];
            if (result != nil) {
                return result;
            }
        }
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM