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

又一个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

这个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];


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


非常建议你在闲暇时看一看这个第三方库<Chameleon>
https://github.com/ViccAlexander/Chameleon
很多情况下它能自动完成状态栏的对比色修改


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

UINavigationBar.appearance().barStyle = UIBarStyle.Black

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


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


我咋试着没问题呢。

【热门文章】
【热门文章】