ios – TabBarController上的彩色状态栏,导航栏无法正常工作

前端之家收集整理的这篇文章主要介绍了ios – TabBarController上的彩色状态栏,导航栏无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在这里阅读了很多帖子,并尝试了大多数提到的选项,但没有人为我解决问题.我有一个基于标签栏控制器的应用程序.每个选项卡都是一个UIViewController,顶部有一个导航栏.

将此代码添加到AppDelegate为我提供了带有白色文本的橙色导航栏,但是带有黑色文本的白色状态栏.

  1. [[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
  2. [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
  3. [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

阅读各种页面上的答案建议将以下内容添加到View控制器:

  1. - (UIStatusBarStyle)preferredStatusBarStyle
  2. {
  3. return UIStatusBarStyleLightContent;
  4. }

然后在View Did Load中调用它:

  1. [self setNeedsStatusBarAppearanceUpdate];

这让我得到一个带有白色文本的白色状态栏,我现在如何让状态栏变为橙色以匹配我的导航栏?

对于那些使用导航控制器的人来说,这里提到的解决方案不起作用,我想这是因为我的主控制器是一个标签栏控制器.

有没有人遇到过这个?提前感谢您提出的任何建议/建议.如果需要,我可以提供一个示例应用程序,但可能使用Tab Bar模板快速构建一个,添加导航栏然后粘贴我的代码示例.

等离子体

解决方法

您可以通过它的名称找到statusBar UIVIew并为其着色.将此方法添加到AppDelegate.m并从didFinishLaunchingWithOptions调用它:
  1. - (void)setStatusBarBackgroundColor:(UIColor *)color {
  2.  
  3. UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
  4.  
  5. if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
  6. statusBar.backgroundColor = color;
  7. }
  8. }
  9.  
  10. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  11.  
  12. ...
  13.  
  14. [self setStatusBarBackgroundColor:[UIColor orangeColor]];
  15.  
  16. ...
  17.  
  18. }

注意:商店中有些应用程序使用此方法.所以苹果HIG政策也没关系.

猜你在找的iOS相关文章