美文网首页
ios 状态栏statusBar的背景颜色

ios 状态栏statusBar的背景颜色

作者: 此笙吥涣 | 来源:发表于2018-03-16 15:50 被阅读0次

    一、无导航条的情况:

    系统默认状态栏的字体颜色为黑色,即UIStatusBarStyle=UIStatusBarStyleDefault,同时背景颜色和self.view.backgroundColor颜色一致,如下图所示:

    [图片上传失败...(image-12ab0d-1521186546600)]

    假如我想让状态栏颜色设置成红色,字体仍为黑色,可以在需要显示的那一页进行如下设置:(最好写在viewWillAppear里面)

    //设置状态栏颜色
    - (void)setStatusBarBackgroundColor:(UIColor *)color {
    
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = color;
        }
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self setStatusBarBackgroundColor:[UIColor redColor]];
        self.view.backgroundColor = [UIColor yellowColor];
    }
    
    

    效果如下:

    [图片上传失败...(image-db2078-1521186546600)]

    假如此时我想让状态栏文字颜色变成白色,可以这样操作:
    在上面代码的基础上再添加下面一段代码:

    - (UIStatusBarStyle)preferredStatusBarStyle{
        return UIStatusBarStyleLightContent;
    }
    
    

    效果如下:

    [图片上传失败...(image-4f1242-1521186546600)]

    问题来了,当你在这一页点击按钮进入下一页后,状态栏背景颜色不变,还是红色,而字体颜色却变成黑色了,比较闹心!可以通过下面的方法随心所欲的在任意一页修改状态栏的字体颜色(字体颜色只有白色和黑色)和背景颜色(直接复制到项目中即可

    //设置字体颜色
    - (UIStatusBarStyle)preferredStatusBarStyle{
        return UIStatusBarStyleLightContent;//白色
    }
    
    //设置状态栏颜色
    - (void)setStatusBarBackgroundColor:(UIColor *)color {
    
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = color;
        }
    }
    //!!!重点在viewWillAppear方法里调用下面两个方法
    -(void)viewWillAppear:(BOOL)animated{
        [self preferredStatusBarStyle];
        [self setStatusBarBackgroundColor:[UIColor redColor]];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor yellowColor];
      }
    
    

    下面的效果是第一页要红底白字,第二页要绿底黑字,返回后也是正常显示

    [图片上传失败...(image-f9f5e2-1521186546600)]

    [图片上传失败...(image-dc1f4e-1521186546600)]

    二、有导航条的情况

    当我在上面的基础上添加了导航条后,会发现字体颜色由之前的白色变成黑色了,背景颜色倒没有发生变化

    [图片上传失败...(image-f519b9-1521186546600)]

    不用担心,可以通过下面的方法完美解决:

    //设置状态栏颜色
    - (void)setStatusBarBackgroundColor:(UIColor *)color {
    
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = color;
        }
    }
    -(void)viewWillAppear:(BOOL)animated{
    
        [self setStatusBarBackgroundColor:[UIColor redColor]];
         [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
    }
    --->!!!同时别忘了在info plist里面将View controller-based status bar appearance设置成NO,(默认是YES)
    
    
    现在基本的设备都适配ios7以上设备,默认的状态栏字体颜色是黑色
    [UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleDefault;
    现在基本的设备都适配ios7以上设备,默认的状态栏字体颜色是黑色
    [UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
    
    

    [图片上传中...(image-e7a36e-1521186546600-3)]

    [图片上传失败...(image-954034-1521186546600)]

    Demo下载地址:https://github.com/zhuchenglong/StatusBarDemo

    以下是我在实际项目中使用的:

    //设置状态栏颜色
    - (void)setStatusBarBackgroundColor:(UIColor *)color {
    
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        NSLog(@"statusBar.backgroundColor--->%@",statusBar.backgroundColor);
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = color;
        }
    }
    
    - (UIStatusBarStyle)preferredStatusBarStyle{
        return UIStatusBarStyleLightContent;//白色
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //Y起点在导航条下面
        self.edgesForExtendedLayout = UIRectEdgeNone;
        //设置navigationItem返回的文字
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
        self.navigationItem.backBarButtonItem = item;
    }
    
    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
    
     //设置导航条透明度
        self.navigationController.navigationBar.translucent = NO;//不透明
        [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];
        //图标颜色为黑色
        [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
        //导航栏背景颜色
        [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
        //导航条下面的黑线
        self.navigationController.navigationBar.clipsToBounds = NO;
    
        //刷新状态栏背景颜色
       // [self setNeedsStatusBarAppearanceUpdate];
    
        //设置状态栏颜色
        [self setStatusBarBackgroundColor:[UIColor blackColor]];
    }
    //一定要在viewWillDisappear里面写,如果写在viewDidDisappear里面会出问题!!!!
    - (void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        //为了不影响其他页面在viewDidDisappear做以下设置
        self.navigationController.navigationBar.translucent = YES;//透明
        [self setStatusBarBackgroundColor:[UIColor clearColor]];
    }
    
    

    相关文章

      网友评论

          本文标题:ios 状态栏statusBar的背景颜色

          本文链接:https://www.haomeiwen.com/subject/rrcrqftx.html