美文网首页
顶部状态栏颜色

顶部状态栏颜色

作者: i爱吃土豆的猫 | 来源:发表于2020-02-29 21:46 被阅读0次

//设置状态栏颜色

- (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;
}

相关文章

网友评论

      本文标题:顶部状态栏颜色

      本文链接:https://www.haomeiwen.com/subject/qhxihhtx.html