简体   繁体   English

动态更改状态栏文本颜色

[英]Dynamically change status bar text color

I know I can change the iPhone's status bar text color, using methods described here . 我知道可以使用此处介绍的方法更改iPhone的状态栏文本颜色。

However, my app has different themes, and I need to update the status bar accordingly. 但是,我的应用程序有不同的主题,我需要相应地更新状态栏。

Calling 调用

[self setNeedsStatusBarAppearanceUpdate];

and

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
}

will obviously not work. 显然是行不通的。 However, it needs to be local, as in only for a specific TabBar view. 但是,它必须是本地的,就像仅在特定的TabBar视图中一样。

First, select your Project and in the Genernal tab you will see something like this. 首先,选择您的Project然后在“ Genernal选项卡中将看到类似这样的内容。

enter image description here 在此处输入图片说明

Then set Status Bar Style to Light just like the image. 然后将“ Status Bar Style设置为“ Light ,就像图像一样。

Second, set View controller-based status bar appearance equal to NO in Info.plist. 其次,在Info.plist中将View controller-based status bar appearance equalNO If you can't find it, just add a new row and set it like the above step. 如果找不到,只需添加新行并按照上述步骤进行设置即可。

And then, run your app you will see the Status bar text colour is white. 然后,运行您的应用程序,您将看到状态栏文本颜色为白色。 :) :)

Just for the ones who come here for the title. 只为那些来这里争夺冠军的人。 You can set a flag and toggle between the status bar text colors like in the code segment given: 您可以设置一个标志并在状态栏文本颜色之间切换,例如在给定的代码段中:

-(UIStatusBarStyle)preferredStatusBarStyle {

if (barStyleLight){

    return UIStatusBarStyleLightContent;
}
else {

    return UIStatusBarStyleDefault;
}

} }

Also note that, this preferredStatusBarStyle method is called whenever we call: [self setNeedsStatusBarAppearanceUpdate]; 另请注意,每当我们调用以下方法时,都会调用此preferredStatusBarStyle方法: [self setNeedsStatusBarAppearanceUpdate];

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM