Passing touch events to iOS status bar

后端 未结 3 1661
误落风尘
误落风尘 2021-01-03 12:38

I have a UIWindow with windowLevel set to UIWindowLevelStatusBar+1. This window has a single semi-transparent view that blocks the status bar. I need to sometimes pass touch

相关标签:
3条回答
  • 2021-01-03 13:04

    As far as I understand this, you should use - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event to implement that behavior. Basically, you either return self if you want to handle the touch event or [super hitTest:point withEvent:event] to let the status bar handle the touch event.

    Check out the UIView Class Reference for more.

    EDIT: As Jonathan mentioned, Apple might not approve this.

    0 讨论(0)
  • 2021-01-03 13:15

    You might find this component over on github helpful.

    Otherwise, Cocoa with Love blog post is really useful to read perhaps.

    0 讨论(0)
  • 2021-01-03 13:20

    So, it seems to be doable with a custom subclass of UIWindow overriding hitTest:withEvent: that manually detects a touch in the subview, and always returns nil.

    - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        if ([event type]==UIEventTypeTouches) {
            UIView *v=[super hitTest:point withEvent:event];
            if (customSubViewthatCoversStatusBarOnly==v) 
                //doLotsOfCoolStuff
        }
    
        return nil;
    }
    

    Status bar recognizes all touches, so there is no breakage with scroll-to-top, return to call, VoiceOver, etc.. And I still get to intercept taps on statusbar.

    I hacked this up just now. I will probably upload an update to App Store later this week with a more mature version of this, will see how much complaining Apple will do.

    EDIT - 7th April:
    Was approved by Apple. Works flawlessly.

    0 讨论(0)
提交回复
热议问题