zoukankan      html  css  js  c++  java
  • ios 状态栏statusBar的背景颜色

    ios 状态栏statusBar的背景颜色

    一、无导航条的情况:

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


     
    14F49066-52A9-4892-AF66-D2F9ED0D9001.png

    假如我想让状态栏颜色设置成红色,字体仍为黑色,可以在需要显示的那一页进行如下设置:(最好写在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];
    }
    

    效果如下:

     
    50B2AB94-49CE-49AB-87A0-E6326C1CE00B.png

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

    - (UIStatusBarStyle)preferredStatusBarStyle{
        return UIStatusBarStyleLightContent;
    }
    

    效果如下:


     
    ED915548-3B47-45EE-AA27-7D068881A946.png

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

    //设置字体颜色
    - (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];
      }
    

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


     
    9B2DA0CD-C106-4255-BA02-F34F55D59E99.png
     
    50007497-1481-4F1E-9852-81BFD9CA7B9C.png

    二、有导航条的情况

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


     
    46CA6B3F-45D6-4628-9D34-0967320616D0.png

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

    //设置状态栏颜色
    - (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;
    
     
    EC111815-4423-479F-BCBD-ADAE52F34E7C.png
     
    54BC597D-F8B3-4EB5-BD0C-394AC0439073.png

    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]];
    }
    

    效果:


     
    81331546-9FF9-4314-96D7-29798D9034B0.png
     
    13D7939F-E318-471C-9EBC-E1E92E619685.png
  • 相关阅读:
    表设计二,联接查询
    表设计一,联接查询
    聚合函数,更新和删除
    Asp.Net MVC项目中如何调试ActiveX插件
    vs2019发布Web到云服务器(IIS)
    event.getRawX()和event.getX()的区别
    android 设备标识
    依赖注入那些事儿
    关于心跳包
    TCP的三次握手(建立连接)和四次挥手(关闭连接)
  • 原文地址:https://www.cnblogs.com/Yishu/p/9925145.html
Copyright © 2011-2022 走看看