iOS 13 更改状态栏背景

项目中需要更改 statusBar 背景色,可是 iOS 13 statusBar 找不到了,原来使用的 kvc [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"] 会造成崩溃。

针对 iOS 13 做一下适配,自己手动添加一个 statusBar:

-(UIView *)statusBarUIview{
    if(@available(iOS 13,*)){
        
        int tag = 13004352;
        
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        UIView *view = [window viewWithTag:tag];
        if (view) {
            return view;
        }else{
            CGRect statusBarRect = [UIApplication sharedApplication].statusBarFrame;
            UIView *statusBarView = [[UIView alloc]initWithFrame:statusBarRect];
            statusBarView.tag = tag;
            [window addSubview:statusBarView];
            return statusBarView;
        }
    
    }else{
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            return statusBar;
        }
    }
    
    return nil;
}

iOS 13 之前还是原来的方法,iOS 13 之后自己手动配置一个。

你可能感兴趣的:(iOS 13 更改状态栏背景)