iOS 7:模态视图控制器状态栏颜色错误但普通视图控制器正确

cha*_*bag 20 statusbar uinavigationbar ios7

我在iOS7中遇到一个问题,普通的UINavigationController推送视图控制器具有UINavigationController导航栏颜色的正确状态栏文本颜色(浅灰色,几乎是白色,因此状态栏文本为黑色).但是,当使用"模态"视图控制器时-presentViewController:animated:completion:,状态栏文本颜色将更改为白色,并且在给定导航栏颜色的情况下很难看到.Navbar颜色在整个应用程序中始终相同,并且不会针对每个视图控制器进行更改.每次-presentViewController通话都会发生这种情况

"查看基于控制器的状态栏外观"设置为YES.

我不知道该尝试解决这个问题.

小智 14

将YourModalViewControler.modalPresentationCapturesStatusBarAppearance设置为YES并将"查看基于控制器的状态栏外观"保持为YES.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.modalPresentationCapturesStatusBarAppearance = YES;
    ....
}
Run Code Online (Sandbox Code Playgroud)

然后覆盖preferredStatusBarStyle

- (UIStatusBarStyle)preferredStatusBarStyle {
    return TheStyleYouWant;
}
Run Code Online (Sandbox Code Playgroud)


Sco*_*ets 13

导航控制器根据其导航栏的barStyle属性决定是否具有浅色或深色内容.默认值,UIBarStyleDefault表示导航栏具有浅色,状态栏将具有深色内容.更改此属性UIBarStyleBlack实际上不会使导航栏变黑(导航栏的颜色仍然使用barTintColor),但它告诉它它有一个深色.然后导航控制器决定,由于导航栏是暗的,它应该将状态栏内容设置为浅.

看来,在您的主导航控制器上(你推动的东西),barStyle确实设置在UIBarStyleBlack某个地方.您必须对模态显示的导航控制器执行相同的操作,如下所示:

UINavigationController *newViewController = [[UINavigationController alloc] initWithRootViewController:modalViewController];
newViewController.navigationBar.barStyle = self.navigationController.navigationBar.barStyle;
[self presentViewController:newViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)


小智 2

您可以在导航控制器类中重新定义preferredStatusBarStyle方法

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
// or UIStatusBarStyleBlackOpaque, UIStatusBarStyleBlackTranslucent, or UIStatusBarStyleDefault

}
Run Code Online (Sandbox Code Playgroud)

您还可以定义“视图加载方法”来设置您想要的自定义颜色

- (void) viewDidLoad
{
    UIColor *barColor = [UIColor whitecolor];

    UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0.f, -20.f, 320.f, 64.f)];
    colorView.opaque = NO;
    colorView.alpha = .5f;
    colorView.backgroundColor = barColor;

    self.navigationBar.barTintColor = barColor;
    self.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    /*self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
     self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
     [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
     self.navigationController.navigationBar.translucent = NO;*/

    [self.navigationBar.layer insertSublayer:colorView.layer atIndex:0];

}
Run Code Online (Sandbox Code Playgroud)