虽然用户看来,iOS7默认样式的状态栏和导航栏时连在一起的,但是实际上导航栏的位置和大小是和之前系统版本一样的,依然是贴在状态栏下面,依然是高 44px;之所以用户看来它们是连在一起,这是因为UINavigationBar里面的_UINavigationBarBackground定位在y 方向-20px的位置,然后高度增加到64px,这样就可以同时充当了两者的背景。
而变透明之后就很容易和后面的内容混淆,虽说一般应用不会把内容和状态栏叠合在一起,但是至少,现在的情况是,默认是会叠合的,开发需要从20px像素以下开始布局页面元素才能避免。
[navCtrl.navigationBar setBackgroundImage:[
UIImage
imageNamed:@
"nav_background"
] forBarMetrics:
UIBarMetricsDefault
];
是一样的,
This is the default behavior for UIViewController on iOS 7. The view will be full-screen which means the status bar will cover the top of your view.
If you have a UIViewController within a UINavigationController and the navigationBar is visible, you can have the following code in your viewDidLoad or have a background image for navigationBar do the trick.
self.edgesForExtendedLayout =UIRectEdgeNone;
If you have navigationBar hidden, then you have to adjust all the UIView elements by shifting 20 points. I dont't see any other solution. Use auto layout will help a little bit.
Here is the sample code for detecting the iOS version, if you want to backward compatibility.
-(void) viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0){CGRect screen =[[UIScreen mainScreen] bounds];if(self.navigationController){CGRect frame =self.navigationController.view.frame;
frame.origin.y =20;
frame.size.height = screen.size.height -20;self.navigationController.view.frame = frame;}else{if([self respondsToSelector:@selector(containerView)]){UIView*containerView =(UIView*)[self performSelector:@selector(containerView)];CGRect frame = containerView.frame;
frame.origin.y =20;
frame.size.height = screen.size.height -20;
containerView.frame = frame;}else{CGRect frame =self.view.frame;
frame.origin.y =20;
frame.size.height = screen.size.height -20;self.view.frame = frame;}}}}
苹果为了让深色浅色背景均能让状态栏内容清晰显示,提供两种状态栏样式:
UIStatusBarStyleDefault = 0 黑色文字,浅色背景时使用
UIStatusBarStyleLightContent = 1 白色文字,深色背景时使用
而以下两个旧状态栏样式将被废弃:
UIStatusBarStyleBlackTranslucent = 1
UIStatusBarStyleLightContent = 2
还有,iOS7中我们通过ViewController重载方法返回枚举值的方法来控制状态栏的隐藏和样式。
首先,需要在Info.plist配置文件中,增加键:
UIViewControllerBasedStatusBarAppearance,并设置为YES;
然后,在UIViewController子类中实现以下两个方法:
1 | - ( UIStatusBarStyle )preferredStatusBarStyle |
3 | return UIStatusBarStyleLightContent ; |
6 | - ( BOOL )prefersStatusBarHidden |