如何将UIView覆盖到状态栏之上

作者: kikido | 来源:发表于2016-10-20 14:26 被阅读2224次

最近在做模仿新浪微博的图片浏览系统,快要完成的时候发现状态栏并没有完全的隐藏掉.

在充电状态下还是能看到状态栏

原来的代码是

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
}

查找资料,才知道

通过[UIApplication sharedApplication].keyWindow获取得到的UIWindow不一定是在界面的最上面.UIWindow有一个UIWindowLevel的属性,该属性定义了UIWindow的层级,系统定义的WindowLevel一共有3种取值:

  • UIWindowLevelNormal
  • UIWindowLevelAlert
  • UIWindowLevelStatusBar

将下面这些代码输出:

NSLog(@"UIWindowLevelNormal==%f, UIWindowLevelAlert==%f, UIWindowLevelStatusBar==%f",UIWindowLevelNormal,UIWindowLevelAlert,UIWindowLevelStatusBar);

得到的结果是:

打印结果

所以,重点就是你只要通过修改Window的UIWindowLevel属性,就能够使得添加到UIWindow上的UIView覆盖到状态栏只上了.

这是修改之后的代码:

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.windowLevel = UIWindowLevelAlert;
    self.frame = window.bounds;
    [window addSubview:self];
}

效果:

改变UIWindowLevel之后的效果

参考资料

作者:唐巧 书籍:iOS开发进阶

相关文章

网友评论

本文标题:如何将UIView覆盖到状态栏之上

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