又一个iOS坑 状态栏文字无论怎么设都还是黑色

如下图,想把顶部的状态栏设置为白色,这样才不会有违和感,但是怎么设置都是黑色。
图片描述

viewController代码如下:


#import "MEViewController.h"

@interface MEViewController ()

@end

@implementation MEViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //设置导航颜色
    [self.navigationController.navigationBar setBarTintColor:[[UIColor alloc]initWithRed:251.0/256.0 green:125/256.0 blue:84/256.0 alpha:1.0]];
    
    //添加底图,防止push页面时,底部黑色
    UIView * navBg = [[UIView alloc]initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, 64)];
    navBg.backgroundColor = [[UIColor alloc]initWithRed:251.0/256.0 green:125/256.0 blue:84/256.0 alpha:1.0];
    [self.view addSubview:navBg];
    
    //取消导航透明
    self.navigationController.navigationBar.translucent = NO;
    
    
    //导航标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    //去底部黑线
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        NSArray *list=self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=(UIImageView *)obj;
                NSArray *list2=imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2=(UIImageView *)obj2;
                        imageView2.hidden=YES;
                    }
                }
            }
        }
    }
    //修改状态栏颜色
    [self setNeedsStatusBarAppearanceUpdate];
    
}

//状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle
{
    DLog("被调用");
    return UIStatusBarStyleLightContent;
    
}

@end
  • 这里preferredStatusBarStyle怎么弄都不会被调用。
    下面是我的info.plist设置

图片描述

这个viewController继承的是UIViewController,他的parentViewController是一个UINavigationViewController。
我在想是不是UINavigationController搞的鬼

问题解决了,感谢@君赏 提供的答案。

果然是UINavigationController在搞鬼。
需要实现UINavigationController的childViewControllerForStatusBarStyle方法,
实现如下

#import "StatusBarNavigationController.h"

@interface StatusBarNavigationController ()

@end

@implementation StatusBarNavigationController

- (UIViewController *)childViewControllerForStatusBarStyle
{
    return self.childViewControllers[0];
}

@end

因为我的UINavigationController只有一个childViewController
所以直接就用 self.childViewControllers[0];

阅读 7.1k
6 个回答

UINavigationControlle1r托管的页面需要实现preferredStatusBarStyle这个方法,需要在UINavifgationController子类重写childViewControllerForStatusBarStyle这个方法。此方法对于IOS7.0以上有效果

你试一下这个呢?在AppDelegate的didFinishLaunchingWithOptions里面加这个试一下呢

UINavigationBar.appearance().barStyle = UIBarStyle.Black
新手上路,请多包涵

真机测试过么?以那个为准

[self setNeedsStatusBarAppearanceUpdate];挪到viewWillAppear:里试下呢?

我咋试着没问题呢。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题