顶部状态栏颜色

2020-02-29  本文已影响0人  i爱吃土豆的猫

//设置状态栏颜色

- (void)setupStatusBarColor:(UIColor *)color  {
   KDeviceOrientationSet;
   //    if (KShuScreen) {
    if (@available(iOS 13.0, *)) {// iOS 13 不能直接获取到statusbar 手动添加个view到window上当做statusbar背景
         if (!self.customizedStatusBar) {
             //获取keyWindow
             UIWindow *keyWindow = [self getKeyWindow];
             self.customizedStatusBar = [[UIView alloc] initWithFrame:keyWindow.windowScene.statusBarManager.statusBarFrame];
                 [keyWindow addSubview:self.customizedStatusBar];
         }
     } else {
       self.customizedStatusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    }
     if ([self.customizedStatusBar respondsToSelector:@selector(setBackgroundColor:)]) {
         self.customizedStatusBar.backgroundColor = color;
     }
//    }
}

//界面销毁时把添加的状态栏从window上移除

- (void)dealloc{
if (@available(ios 13.0, *)) {
    if (self.customizedStatusBar) {
        [self.customizedStatusBar removeFromSuperview];
        self.customizedStatusBar = nil;
    }
}
}

- (UIWindow *)getKeyWindow{
  //获取keywindow
  NSArray *array = [UIApplication sharedApplication].windows;
  UIWindow *window = [array objectAtIndex:0];
  if (!window.hidden || window.isKeyWindow) { //  判断取到的window是不是keywidow
    return window;
}
//如果上面的方式取到的window 不是keywidow时通过遍历windows取keywindow
  for (UIWindow *window in array) {
        if (!window.hidden || window.isKeyWindow) {
            return window;
        }
   }
    return nil;
}
上一篇 下一篇

猜你喜欢

热点阅读