如下图,想把顶部的状态栏设置为白色,这样才不会有违和感,但是怎么设置都是黑色。
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
这里preferredStatusBarStyle怎么弄都不会被调用。
下面是我的info.plist设置
这个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];
由
UINavigationControlle
1r托管的页面需要实现preferredStatusBarStyle
这个方法,需要在UINavifgationController
子类重写childViewControllerForStatusBarStyle
这个方法。此方法对于IOS7.0以上有效果