有兩種方法可以控制狀態(tài)欄的顏色及可見性
一 使用UIApplication
相關(guān)方法
- 前提: 需要在
Info.plist
文件中設(shè)置View controller-based status bar appearance
為NO
. - 注意:IOS9已經(jīng)把這個方法聲明為廢棄方法了.
- 代碼如下
- (IBAction)changeColor:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 0) {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
} else {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}
}
- (IBAction)setHiddenStatus:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 0) {
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
} else {
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
}
二 使用UIViewController
相關(guān)方法
- 前提:需要在
Info.plist
文件中設(shè)置View controller-based status bar appearance
為YES
. 或者去掉這個鍵值對逸爵,默認為YES
. - 代碼如下
- (IBAction)changeColor:(UISegmentedControl *)sender {
[self setNeedsStatusBarAppearanceUpdate];
}
- (IBAction)setHiddenStatus:(UISegmentedControl *)sender {
[self setNeedsStatusBarAppearanceUpdate];
}
#pragma mark - UIViewController 相關(guān)方法
- (UIStatusBarStyle)preferredStatusBarStyle
{
if (self.colorSegmentControl.selectedSegmentIndex == 0) {
return UIStatusBarStyleDefault;
}
return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
if (self.hiddenSegmentControl.selectedSegmentIndex == 0) {
return NO;
}
return YES;
}
//狀態(tài)欄隱藏或顯示的動畫
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationFade;
}