修改单个页面状态栏背景色

- (void)setStatusBarBackgroundColor:(UIColor *)color {

    if(@available(iOS13.0, *)) {

        staticUIView*statusBar =nil;

        if(!statusBar) {

            staticdispatch_once_tonceToken;

            dispatch_once(&onceToken, ^{

                statusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;

                [[UIApplication sharedApplication].keyWindow addSubview:statusBar];

                statusBar.backgroundColor= color;

            });

        }else{

            statusBar.backgroundColor= color;

        }

    }else{

        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

        if([statusBarrespondsToSelector:@selector(setBackgroundColor:)]) {

            statusBar.backgroundColor= color;

        }

    }

}

//设置字体颜色

- (UIStatusBarStyle)preferredStatusBarStyle{

    return UIStatusBarStyleLightContent;//白色

}

- (void)viewWillAppear:(BOOL)animated{

    [superviewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:NO];

    [self preferredStatusBarStyle];

    [self setStatusBarBackgroundColor:[UIColor colorWithHexString:@"#2E6ABC"]];

}

- (void)viewWillDisappear:(BOOL)animated{

    [superviewWillDisappear:animated];

    [self setStatusBarBackgroundColor:NAVTHEME_COLOR];

你可能感兴趣的:(修改单个页面状态栏背景色)