iOS development sets status bar font color

The font of the status bar is black: UIStatusBarStyleDefault
The font of the status bar is white: UIStatusBarStyleLightContent

1. In info.plist, set View controller-based status bar appearance to NO**

The color of the status bar font is only set by the following properties, and defaults to white:

// default is UIStatusBarStyleDefault
[UIApplication sharedApplication].statusBarStyle

How to solve the problem of different status bar font colors in individual VCs

  1. In info.plist, set View controller-based status bar appearance to NO.
  2. In the app delegate: In the app delegate:
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

3. In some vcs where the font color of the status bar is different

- (void)viewWillAppear:(BOOL)animated{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

2. In info.plist, set View controller-based status bar appearance to YES, or not set it.

The default value of View controller-based status bar appearance is YES.
If View controller-based status bar appearance is YES.
Then [UIApplication sharedApplication].statusBarStyle is invalid.

Use the following method:
1. Rewrite the preferredStatusBarStyle method of vc in vc.

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

2. Call in viewDidload: [self setNeedsStatusBarAppearanceUpdate];
However, when vc is in nav, the above method is useless, and the preferredStatusBarStyle method in vc does not need to be called at all.
The reason is that after [self setNeedsStatusBarAppearanceUpdate] is issued,
only the preferredStatusBarStyle method in the navigation controller will be called, and
the preferredStatusBarStyley method in vc will not be called.

There are two solutions:

method one:

Setting the barStyle property of navbar will affect the font and background color of the status bar. as follows.

//status bar的字体为白色

//导航栏的背景色是黑色。

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

//status bar的字体为黑色

//导航栏的背景色是白色,状态栏的背景色也是白色。

//self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

Method 2:
Customize a subclass of nav bar and override the preferredStatusBarStyle method in this subclass:

MyNav* nav = [[MyNav alloc] initWithRootViewController:vc];

self.window.rootViewController = nav;

@implementation MyNav

- (UIStatusBarStyle)preferredStatusBarStyle
{
    UIViewController* topVC = self.topViewController;
    return [topVC preferredStatusBarStyle];
}

Guess you like

Origin blog.csdn.net/biyuhuaping/article/details/85709521