前段時間項目中出現一個十分詭異的bug,打電話、分享熱點開啓之後,再打開APP,然後關閉電話或熱點,APP導航條上端有20像素的黑條產生,具體表現如下:
經查找資料調試發現,狀態欄標準高度爲20,但是當打電話、分享熱點時,狀態欄的高度有20變爲40,這時APP頁面整體下壓20。當打電話、分享熱點結束時,狀態欄高度變回20,本來頁面應該上彈20的,但是我們的APP有一個抽屜,抽屜是主頁的childViewController,這個childViewController沒有彈回去,我們需要手動將其彈回。
我們可以監聽狀態欄高度變化的事件,該事件名稱爲:UIApplicationDidChangeStatusBarFrameNotification,在需要監聽的地方,添加:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(layOutControllerViews) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkStatusBarNormal) name:@"enterForeground" object:nil];
並且監聽進入APP之後狀態欄高度的是否是標準的,如果是標準的,則不用做任何處理,如果不是標準的的,則需要將childViewController向上拉20個高度:
- (void)checkStatusBarNormal {
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
CGFloat statusBarHeight = statusBarFrame.size.height;
if (statusBarHeight == 20.0) {
self.isStatusBarNormal = YES;
} else {
self.isStatusBarNormal = NO;
}
}
- (void)layOutControllerViews {
CGRect mainFrame = _mainVc.view.frame;
CGRect leftMenuFrame = self.view.frame;
mainFrame.origin.y = 0;
leftMenuFrame.origin.y = 0;
leftMenuFrame.size.height = SCREEN_HEIGHT;
if (!self.isStatusBarNormal) {
mainFrame.size.height = SCREEN_HEIGHT;
}
_mainVc.view.frame = mainFrame;
self.view.frame = leftMenuFrame;
}
另外,UIApplicationDidChangeStatusBarFrameNotification 還可以用作UI的自適應,當打電話、分享熱點時,可能會把頁面的整體佈局打亂,所以應該根據狀態欄的高度自適應調整UI佈局。
最後測試發現,糗百、招商銀行客戶端都有這個問題,這個bug確實比較難發現,不過還是有可能會出現的,出現之後確實很影響用戶體驗。