iOS7 设置状态栏文字颜色

在info.plist中,将View controller-based status bar appearance设置不同值,有不同的处理方法

一、将View controller-based status bar appearance设为YES或者没有设置

View controller-based status bar appearance的默认值就是YES,如果View controller-based status bar appearance为YES,那么下面这个方法无效

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];

需要在VC中做一些处理

1、在VC中重写preferredStatusBarStyle

- (UIStatusBarStyle) preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}
2、在VC中调用 [ self  setNeedsStatusBarAppearanceUpdate ]方法来更新状态栏字体,该方法会触发VC中的preferredStatusBarStyle方法。

但是,当VC在UINavigationController中时,上面方法无效,VC中的preferredStatusBarStyle方法不会被调用。

原因在于preferredStatusBarStyle在UINavigationController被调用,VC中的preferredStatusBarStyle方法不会被调用。

解决办法有2个:

方法1:设置self.navigationController.navigationBar的barStyle,该属性会影响状态栏的字体何背景色。     

// 状态栏文字颜色将改变成白色
// 导航栏的背景色将改变成黑色。
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
// 状态栏文字颜色将改变成黑色
// 导航栏的背景色将改变成白色
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

方法2:在UINavigationController中重写preferredStatusBarStyle方法。

- (UIStatusBarStyle)preferredStatusBarStyle
{
    UIViewController* visibleViewController = self.visibleViewController;
    return [visibleViewController preferredStatusBarStyle];
}


二、将View controller-based status bar appearance设为NO

状态栏字体的颜色只由下面属性设定,默认是黑色:

// Dark content, for use on light backgrounds 
// default is UIStatusBarStyleDefault
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];

解决个别VC中状态栏字体颜色不同
1、在info.plist中,将View controller-based status bar appearance设为NO。
2、在App Delegate中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    
    return YES;
}
3、在VC中处理状态栏颜色字体
-(void)viewWillAppear:(BOOL)animated
{
    // Super viewWillAppear
    [super viewWillAppear:animated];
    _previousStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:animated];
    
    // Super viewWillDisappear
    [super viewWillDisappear:animated];
}

在VC中更完整的代码为
@interface ViewController ()
@property (nonatomic,assign) UIStatusBarStyle previousStatusBarStyle;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    // Super viewWillAppear
    [super viewWillAppear:animated];
    _previousStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:animated];
    
    // Super viewWillDisappear
    [super viewWillDisappear:animated];
}
@end








     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值