如何防止UINavigationBar阻止视图上的触摸

时间:2014-02-17 08:03:01

标签: ios iphone cocoa-touch uiview

我有一个UIView部分卡在UINavigationBar UIViewController下面,处于全屏模式。 UINavigationBar阻止此视图触及它覆盖它的部分。我希望能够解锁这些视图并让它们通过。我已经使用以下内容对UINavigationBar进行了子类化:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];

    if (view.tag == 399)
    {
        return view;
    }
    else
    {
        return nil;
    }
}

...我用数字399标记了有问题的视图。是否有可能通过这个视图的触摸而没有指向它的指针(例如我在上面如何标记它)?关于如何使用hittest方法(或者如果它甚至可能)使这项工作有点困惑。

4 个答案:

答案 0 :(得分:6)

对UINavigationBar进行子类化并覆盖- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event,使得它为您要接收的视图所触摸的矩形返回NO,否则为YES

例如:

UINavigationBar子类.h:

@property (nonatomic, strong) NSMutableArray *viewsToIgnoreTouchesFor; 

UINavigationBar子类.m:

- (NSMutableArray *)viewsToIgnoreTouchesFor
{
    if (!_viewsToIgnoreTouchesFor) {
        _viewsToIgnoreTouchesFor = [@[] mutableCopy];
    }
    return _viewsToIgnoreTouchesFor;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL pointInSide = [super pointInside:point withEvent:event];
    for (UIView *view in self.viewsToIgnoreTouchesFor) {

        CGPoint convertedPoint = [view convertPoint:point fromView:self];
        if ([view pointInside:convertedPoint withEvent:event]) {
            pointInSide = NO;
            break;
        }
    }

    return pointInSide;
}

在您的全屏viewController中,您可以在navBar后面看到这些行,将这些行添加到viewDidLoad

UINavigationBarSubclass *navBar = 
(UINavigationBarSubclass*)self.navigationController.navigationBar;
[navBar.viewsToIgnoreTouchesFor addObject:self.buttonBehindBar];

请注意:这不会向navigationBar发送触摸,这意味着如果您添加一个位于导航栏按钮后面的视图,导航栏上的按钮将不会接收到触摸。

夫特:

var viewsToIgnore = [UIView]()

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {

    let ignore = viewsToIgnore.first {
        let converted = $0.convert(point, from: self)
        return $0.point(inside: converted, with: event)
    }
    return ignore == nil && super.point(inside: point, with: event)
}

有关pointInside:withEvent:

的更多信息,请参阅documentation

如果pointInside:withEvent:无效,您可能需要在hitTest:withEvent:中尝试上述代码。

答案 1 :(得分:2)

这是一个版本,不需要在下面设置要启用的特定视图。相反,它允许任何触摸通过,除非该触摸发生在UIControl或带有UIGestureRecognizer的视图中。

import UIKit

/// Passes through all touch events to views behind it, except when the
/// touch occurs in a contained UIControl or view with a gesture
/// recognizer attached
final class PassThroughNavigationBar: UINavigationBar {

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        guard nestedInteractiveViews(in: self, contain: point) else { return false }
        return super.point(inside: point, with: event)
    }

    private func nestedInteractiveViews(in view: UIView, contain point: CGPoint) -> Bool {

        if view.isPotentiallyInteractive, view.bounds.contains(convert(point, to: view)) {
            return true
        }

        for subview in view.subviews {
            if nestedInteractiveViews(in: subview, contain: point) {
                return true
            }
        }

        return false
    }
}

fileprivate extension UIView {
    var isPotentiallyInteractive: Bool {
        guard isUserInteractionEnabled else { return false }
        return (isControl || doesContainGestureRecognizer)
    }

    var isControl: Bool {
        return self is UIControl
    }

    var doesContainGestureRecognizer: Bool {
        return !(gestureRecognizers?.isEmpty ?? true)
    }
}

答案 2 :(得分:2)

我修改了Tricky的解决方案以将SwiftUI作为Extension使用。解决这个问题很有用。将这段代码添加到代码库后,所有视图都将能够捕获屏幕顶部的点击。

还发布了此更改to my blog

import UIKit

/// Passes through all touch events to views behind it, except when the
/// touch occurs in a contained UIControl or view with a gesture
/// recognizer attached
extension UINavigationBar {
    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        guard nestedInteractiveViews(in: self, contain: point) else { return false }
        return super.point(inside: point, with: event)
    }

    private func nestedInteractiveViews(in view: UIView, contain point: CGPoint) -> Bool {
        if view.isPotentiallyInteractive, view.bounds.contains(convert(point, to: view)) {
            return true
        }

        for subview in view.subviews {
            if nestedInteractiveViews(in: subview, contain: point) {
                return true
            }
        }

        return false
    }
}

private extension UIView {
    var isPotentiallyInteractive: Bool {
        guard isUserInteractionEnabled else { return false }
        return (isControl || doesContainGestureRecognizer)
    }

    var isControl: Bool {
        return self is UIControl
    }

    var doesContainGestureRecognizer: Bool {
        return !(gestureRecognizers?.isEmpty ?? true)
    }
}

答案 3 :(得分:1)

针对上述Objective C答案的

Swift 解决方案。

class MyNavigationBar: UINavigationBar {

    var viewsToIgnoreTouchesFor:[UIView] = []

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        var pointInside = super.point(inside: point, with: event)

        for each in viewsToIgnoreTouchesFor {
            let convertedPoint = each.convert(point, from: self)
            if each.point(inside: convertedPoint, with: event) {
                pointInside = false
                break
            }
        }
        return pointInside
    }
}

现在从viewDidLoad方法或代码中的任何适用位置设置要在导航栏下方捕获其触摸的视图,如下所示

if let navBar = self.navigationController?.navigationBar as? MyNavigationBar {
    navBar.viewsToIgnoreTouchesFor = [btnTest]
}

触摸愉快!