近在做模仿新浪微博的圖片瀏覽系統,快要完成的時候發現狀態欄并沒有完全的隱藏掉.
在充電狀態下還是能看到狀態欄
原來的代碼是
- (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之后的效果