info.plist文件中凑术,View controller-based status bar appearance項(xiàng)設(shè)為YES,則View controller對status bar的設(shè)置優(yōu)先級高于application的設(shè)置泌豆。為NO則以application的設(shè)置為準(zhǔn),view controller的prefersStatusBarHidden方法無效,是根本不會被調(diào)用的。
一巍棱、如果View controller-based status bar appearance 設(shè)為YES。
這時 view controller中對status bar的設(shè)置優(yōu)先級高于application的設(shè)置蛋欣,用下面的方式隱藏status bar:
1航徙、在view controller中調(diào)用setNeedsStatusBarAppearanceUpdate,更新status bar的顯示陷虎。
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}
}
2到踏、覆蓋view controller的prefersStatusBarHidden的實(shí)現(xiàn)倒得,返會YES。
- (BOOL)prefersStatusBarHidden{
return YES;
}
二夭禽、如果View controller-based status bar appearance 設(shè)為NO,這時application的設(shè)置優(yōu)先級最高霞掺,用下面的方式隱藏status bar:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
綜合上面的結(jié)論,如果iOS6和iOS7都支持讹躯,則處理方式如下:
一菩彬、如果View controller-based status bar appearance 設(shè)為NO。
則在iOS6和iOS7都是用下面的方法隱藏status bar潮梯。
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
二骗灶、如果View controller-based status bar appearance 設(shè)為YES。
則需要判斷當(dāng)前是iOS6還是iOS7秉馏。如果是iOS6耙旦,則還通過sharedApplication隱藏。
如果是iOS7萝究,則用setNeedsStatusBarAppearanceUpdate加prefersStatusBarHidden的方式來隱藏 status bar免都。
取info.plist中 View controller-based status bar appearance中的設(shè)置:
NSNumber *isVCBasedStatusBarAppearanceNum = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
if (isVCBasedStatusBarAppearanceNum) {
_isVCBasedStatusBarAppearance = isVCBasedStatusBarAppearanceNum.boolValue;
} else {
_isVCBasedStatusBarAppearance = YES; // default
}