iOS13: how to detect a status bar click event?

為{幸葍}努か 提交于 2019-12-03 09:40:53

问题


In my appDelegate, I override touchesBegan to detect when the status bar is clicked:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    CGPoint location = [[[event allTouches] anyObject] locationInView:kWindow];
    CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;

    if (CGRectContainsPoint(statusBarFrame, location)) {
        ///do something 
    }
}

Before iOS 13, this worked; on iOS 13, it does not. The method is never executed. How can I fix this?


回答1:


After some research, I found that the status bar is no longer part of the application on iOS13, it’s part of the system UI (“springboard”). You need to access it via UIStatusBarManager

So this no longer works:

CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;

In iOS 13, you need to get the StatusBar frame like this:

UIStatusBarManager *manager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
CGRect statusBarFrame = manager.statusBarFrame;



回答2:


In iOS 13, StatusBar Event was managed by UIStatusBarManager, these methods will be called when click the StatusBar:

-[UIStatusBarManager _handleScrollToTopAtXPosition:]
-[UIStatusBarManager handleTapAction:]

So, you can hook -[UIStatusBarManager handleTapAction:] to detecte StatusBar click event



来源:https://stackoverflow.com/questions/57324218/ios13-how-to-detect-a-status-bar-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!