32

I have two windows -- a front one, and a back one. The front one is used to overlay some stuff on top of the back one. I want to be able to capture touches on some parts of the front window, but not others; I want the front window to receive touches in some areas, but pass them through to the back window in others.

Any ideas as to how I'd do this?

3
  • Why are you using 2 windows?
    – rdelmar
    Feb 6, 2013 at 23:54
  • It's a piece of an SDK which displays on overlay over the host app -- we can't touch the host app's view hierarchy. Feb 7, 2013 at 0:07
  • Did you ever find a way to accomplish this? If so, could you at least give us a hint of the path to wander down even if you don't have time to write-up a full answer?
    – Will Moore
    Apr 20, 2013 at 6:49

3 Answers 3

43
+100

Ok, here's what I did: I created two views in the front window. The first view covered the area where I wanted to catch the touches; the second, where I wanted the touches to pass through. I sub-classed UIWindow and overrode the hitTest:withEvent method like so:

- (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {

  // See if the hit is anywhere in our view hierarchy
  UIView *hitTestResult = [super hitTest:point withEvent:event];

  // ABKSlideupHostOverlay view covers the pass-through touch area.  It's recognized
  // by class, here, because the window doesn't have a pointer to the actual view object.
  if ([hitTestResult isKindOfClass:[ABKSlideupHostOverlayView class]]) {

    // Returning nil means this window's hierachy doesn't handle this event. Consequently,
    // the event will be passed to the host window.
    return nil;
  }

  return hitTestResult;
}

And in the class which creates the front window, I used a gesture recognizer to catch touches on the first view.


Same in 2017 code:

class PassView: UIView {}

class UIHigherWindow: UIWindow {

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        let hitView = super.hitTest(point, with: event)
        
        if hitView!.isKind(of: PassView.self) {
            
            return nil
        }
  
        return hitView
    }
}

Your "uber" window will be some view controller, UberVc.

Just make the main view (that is to say, simply the background .view) of UberVc be a PassView.

Then add say buttons etc. on the UberVc.

The above code results in any clicks on the buttons going to UberVc, and any clicks not on the buttons (ie, on the "background") going through to your regular window/VCs.


Just for clarity ...

If you simply want to pass everything through (so, the over-window is perhaps a message only or some sort of temporary thing), it's simply

class PassTroughWindow: UIWindow {
    override func hitTest(_ p: CGPoint, with e: UIEvent?) -> UIView? {
        return nil
    }
}

and you're done.

6
  • does your solution work if it a web view that has a text field on the bottom window? and when you press the "done" button that comes with the keyboard? Sep 22, 2016 at 17:13
  • Don't know -- that answer is three years old. 😝 Give it a try and post your result! 😀 Sep 22, 2016 at 17:15
  • 1
    it didn't :\, thats why I am trying to see if someone figured it out :p Sep 22, 2016 at 17:26
  • 1
    spectacular, Anna. I put in some latest-swift code; feel free to edit/unwind etc of course ... Cheers and thanks!!!
    – Fattie
    Aug 16, 2017 at 18:01
  • 1
    bounty en route for amazing answer!
    – Fattie
    Aug 16, 2017 at 18:01
2

I had a similar issue: the additional UIWindow should have a transparent view on it with some intractable subviews. All touches outside of those intractable views must be passed through.

I ended up using modified version of Anna's code that uses view's tag instead of type checking. This way view subclass creation is not required:

class PassTroughWindow: UIWindow {
    var passTroughTag: Int?

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

        let hitView = super.hitTest(point, with: event)

        if let passTroughTag = passTroughTag {
            if passTroughTag == hitView?.tag {
                return nil
            }
        }
        return hitView
    }
}

Assuming you have a window and root view controller created you you could use it like this:

let window: PassTroughWindow 
//Create or obtain window

let passTroughTag = 42

window.rootViewController.view.tag = passTroughTag
window.passTroughTag = passTroughTag

//Or with a view:
let untouchableView: UIView // Create it
untouchableView.tag = passTroughTag
window.addSubview(untouchableView)
-4

Instead of using multiple UIWindows, get the key window with [[UIApplication sharedApplication] keyWindow], then add your view to it:

mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:view]
1
  • I tried that, but it doesn't work in all cases: a popover can appear over your view. Jun 26, 2013 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.