iOS 獲取點(diǎn)擊頂部狀態(tài)欄的事件

以iOS13為分水嶺,iOS13之前和之后獲取點(diǎn)擊狀態(tài)欄的事件不一樣

iOS13以前例如:iOS12、iOS11、iOS10 獲取狀態(tài)欄點(diǎn)擊事件在AppDelegate添加以下代碼
///iOS13以下用來監(jiān)聽店家狀態(tài)欄的方法
- (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以下用來監(jiān)聽店家狀態(tài)欄的方法");
    }
}
iOS13以后例如:iOS14獲取點(diǎn)擊狀態(tài)欄的事件,就需要新建UIStatusBarManager的分類,由于不能覆蓋分類中UIStatusBarManager的方法,我這里用到了runtime進(jìn)行方法交換。
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:));

    // 交換方法地址,相當(dāng)于交換實(shí)現(xiàn)方式
    method_exchangeImplementations(handleTapActionName, handleTapActionOneName);
}

///獲取到點(diǎn)擊狀態(tài)欄的事件
- (void)handleTapActionOne:(id)arg1 {
    NSLog(@"iOS13.0后點(diǎn)擊狀態(tài)欄");
    //調(diào)系統(tǒng)點(diǎn)擊狀態(tài)欄的方法
    UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
    [statusBarManager handleTapActionOne:arg1];
}

@end

這是具體的Demo,有幫助點(diǎn)個星星? 源碼Demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容