【iOS13问题】修改状态栏的颜色(亲测,有效)

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleLightContent;
    } else {
        return UIStatusBarStyleLightContent;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self setupStatusBarColor:[UIColor whiteColor]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   [self setupStatusBarColor:[UIColor blackColor]];
}

- (void)setupStatusBarColor:(UIColor *)color
{
    if (@available(iOS 13.0, *)) {
          if (!_statusBar) {
              UIWindow *keyWindow = [UIApplication sharedApplication].windows[0];
              _statusBar = [[UIView alloc] initWithFrame:keyWindow.windowScene.statusBarManager.statusBarFrame];
              [keyWindow addSubview:_statusBar];
          }
      } else {
        _statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
     }
      if ([_statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
          _statusBar.backgroundColor = color;
      }
}

- (void)dealloc{
    NSLog(@" dealloc  ");
    if (@available(ios 13.0, *)) {
        if (_statusBar) {
            [_statusBar removeFromSuperview];
            _statusBar = nil;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/xjf125/p/11974100.html