以iOS13为分水岭,iOS13之前和之后获取点击状态栏的事件不一样
iOS13以前例如:iOS12、iOS11、iOS10 获取状态栏点击事件在AppDelegate添加以下代码
///iOS13以下用来监听店家状态栏的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
CGPoint location = [[[event allTouches] anyObject] locationInView:self.window];
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
if (CGRectContainsPoint(statusBarFrame, location)) {
NSLog(@"iOS13以下用来监听店家状态栏的方法");
}
}
iOS13以后例如:iOS14获取点击状态栏的事件,就需要新建UIStatusBarManager的分类,由于不能覆盖分类中UIStatusBarManager的方法,我这里用到了runtime进行方法交换。
UIStatusBarManager.h
@interface UIStatusBarManager (TapStatusEvnet)
- (void)handleTapAction:(id)arg1;
- (void)handleTapActionOne:(id)arg1;
@end
UIStatusBarManager.m
#import "UIStatusBarManager+TapStatusEvnet.h"
#import <objc/runtime.h>
@implementation UIStatusBarManager (TapStatusEvnet)
+ (void)load
{
// 交换方法
// 获取handleTapAction方法地址
Method handleTapActionName = class_getInstanceMethod(self, @selector(handleTapAction:));
// 获取handleTapActionOne方法地址
Method handleTapActionOneName = class_getInstanceMethod(self, @selector(handleTapActionOne:));
// 交换方法地址,相当于交换实现方式
method_exchangeImplementations(handleTapActionName, handleTapActionOneName);
}
///获取到点击状态栏的事件
- (void)handleTapActionOne:(id)arg1 {
NSLog(@"iOS13.0后点击状态栏");
//调系统点击状态栏的方法
UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
[statusBarManager handleTapActionOne:arg1];
}
@end
这是具体的Demo,有帮助点个星星✨ 源码Demo