iOS 7: modal view controller status bar is wrong c

2019-03-14 15:31发布

I have an issue in iOS7 where a normal UINavigationController pushed view controller has the correct status bar text color for the UINavigationController navbar color (which is a light gray, almost white so the status bar text is black). However, when a "modal" view controller is presented using -presentViewController:animated:completion:, the status bar text color is changed to white and is very hard to see given the color of the navbar. Navbar color is always the same across the whole app and does not change for each view controller. This happens on every -presentViewController call.

"View controller-based status bar appearance" is set to YES.

I am not sure what to look at to try and solve this.

9条回答
放我归山
2楼-- · 2019-03-14 15:44

This works. But I'm not happy with it because it a bit hacky. I think it's a bug that preferredStatusBarStyle is not called for modal view. Will ask Apple.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self setNeedsStatusBarAppearanceUpdate];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self setNeedsStatusBarAppearanceUpdate];
}
查看更多
神经病院院长
3楼-- · 2019-03-14 15:46

You can redefine the preferredStatusBarStyle method in your navigation controller class

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

}

and you can also define a "view did load method" to set custom colors you want

- (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];

}
查看更多
乱世女痞
4楼-- · 2019-03-14 15:51

Set UIViewControllerBasedStatusBarAppearance to NO in your info.plist

查看更多
beautiful°
5楼-- · 2019-03-14 15:53

My solution:

newViewController.modalPresentationStyle = .fullScreen

By default, UINavigationController decides the status bar style and overrides all sub view controllers styles. However, when a modal view controller is fullscreen, its method preferredStatusBarStyle is called.

查看更多
Evening l夕情丶
6楼-- · 2019-03-14 15:58

I was having the same problem as you were having. The info.plist was correct and everywhere else preferredStatusBarStyle was called correctly. But not in my modal view. That is because preferredStatusBarStyle was sent to the navigation controller. So I made a subclass of UINavigationController that passed on preferredStatusBarStyle to the view controller it was presenting, and voila, all was behaving as it should again

查看更多
狗以群分
7楼-- · 2019-03-14 16:05

The navigation controller decides whether to have a light or dark content based on its navigation bar's barStyle property. The default, UIBarStyleDefault, means the navigation bar has a light color and the status bar will have dark content. Changing this property to UIBarStyleBlack doesn't actually make the navigation bar black (the color of the navigation bar is still set using barTintColor), but it tells it that it has a dark color. The navigation controller then decides that, since the navigation bar is dark, it should set the status bar content to light.

It appears that on your main navigation controller (on which you push things) the barStyle is indeed set to UIBarStyleBlack somewhere. You have to do the same thing to the modally presented navigation controller, like so:

UINavigationController *newViewController = [[UINavigationController alloc] initWithRootViewController:modalViewController];
newViewController.navigationBar.barStyle = self.navigationController.navigationBar.barStyle;
[self presentViewController:newViewController animated:YES completion:nil];
查看更多
登录 后发表回答