iOS 更改状态栏、导航栏颜色的几种方法

状态栏: 是指iOS屏幕最上面的20像素高的部分,分为前景部分和背景部分

  • 前景部分:包括显示电池、时间等部分;
  • 背景部分:包括显示黑色或者图片的背景部分;

改变状态栏的方法

View controller-based status bar appearance 设置为 NO

1、plist

在plist里增加一行 UIStatusBarStyle,选择UIStatusBarStyleDefault 或者UIStatusBarStyleLightContent

注意:info.plist文件中,View controller-based status bar appearance项设为YES,则View controller对status bar的设置优先级高于application的设置。为NO则以application的设置为准,view controller的prefersStatusBarHidden方法无效,是根本不会被调用的。

2、代码设置

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

View controller-based status bar appearance 设置为 YES 或者默认

1、代码

- (UIStatusBarStyle)preferredStatusBarStyle
{
    //黑色
    //return UIStatusBarStyleDefault;
    //白色
    return UIStatusBarStyleLightContent;
}

2、在导航控制器下

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //设置导航条颜色
    BaseNavigationController *nav = (BaseNavigationController *)self.navigationController;
    nav.statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg"] forBarMetrics:UIBarMetricsDefault];
    //返回按钮
    UIButton *navItem = [UIButton buttonWithType:UIButtonTypeCustom];
    [navItem setImage:[UIImage imageNamed:@"leftArrow"] forState:UIControlStateNormal];
    [navItem addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    navItem.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    navItem.frame = CGRectMake(0, 0, 40, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navItem];;

    //设置状态栏字体颜色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //设置导航条颜色
    BaseNavigationController *nav = (BaseNavigationController *)self.navigationController;
    nav.statusBarStyle = UIStatusBarStyleDefault;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0xFFFFFF"]] forBarMetrics:UIBarMetricsDefault];
    //设置状态栏字体颜色
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}

你可能感兴趣的:(iOS基础(OC),iOS学习笔记)