状态栏样式修改

说明:只是局部修改方案

  1. 修改info.plist
    View controller-based status bar appearance : YES

  2. 重写preferredStatusBarStyle 方法

  • (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.scrollview.contentOffset.y > 0) {
    return UIStatusBarStyleDefault;
    }
    return UIStatusBarStyleLightContent;
    }
  1. 在需要修改的地方增加下面方法
    [self setNeedsStatusBarAppearanceUpdate];

补充:
//设置状态栏背景颜色

  • (void)setStatusBarBackgroundColor:(UIColor *)color {
    if (@available(iOS 13.0, *)) {
    UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame];

         statusBar.backgroundColor = color;
         [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
     } else {
         // Fallback on earlier versions
         UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
            if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
                statusBar.backgroundColor = color;
            }
     }
    

}

你可能感兴趣的:(状态栏样式修改)