
1 NSString *statusBarFrame = NSStringFromCGRect([UIapplication sharedApplication].statusBarFrame); 2 NSLog(@"%@", statusBarFrame);在iPhone 6竖屏测试输出: 2015-08-04 16:33:47.159 Test[6175:205261] {{0, 0}, {375, 20}} 在iPhone 6横屏测试输出: 2015-08-04 16:33:47.159 Test[6175:205261] {{0, 0}, {667, 20}} 在iPhone 6 Plus竖屏测试输出: 2015-08-04 16:33:47.159 Test[6175:205261] {{0, 0}, {414, 20}} 可见其中origin.x和origin.y总是0,size.height总是20,size.width依赖于不同设备及横竖屏。
也可以在General中将Hide status bar勾选:
实际上,上面两种设置方法最终作用到info.plist文件。可以直接修改该文件,如果不嫌麻烦又不担心出错的话。如果没有使用基于ViewController的状态栏控制,并且App内部又需要将状态栏显示出来,可以在AppDelegate中设置:[[UIApplication sharedApplication] setStatusBarHidden:NO];
也可以在General中将Status Bar style选择为Light:
同样的,上面两种设置方法最终作用到info.plist文件。如果没有使用基于ViewController的状态栏控制,并且App内部又需要将状态栏颜色改为黑色,可以在AppDelegate中设置:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
| UIViewController方法 | 说明 |
| - (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO | 询问是否隐藏状态栏。 |
| - (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault | 询问状态栏样式(UIStatusBarStyleDefault/UIStatusBarStyleLightContent)。 |
| // Override to return the type of animation that should be used for status bar changes for this view controller. This currently only affects changes to prefersStatusBarHidden. - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarAnimationFade | 询问状态栏显示或隐藏动画。 |
| // This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block. - (void)setNeedsStatusBarAppearanceUpdate NS_AVAILABLE_IOS(7_0); |
设置需要更新状态栏。主动调用该方法,将间接调用上述三个方法。如果需要动画生效,需:
[UIView animateWithDuration:0.4 animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }]; |
| UIApplication方法/属性 | 说明 |
| // Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system. @property(nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden; - (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_AVAILABLE_IOS(3_2); | 设置是否隐藏状态栏。 |
| // Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system. @property(nonatomic) UIStatusBarStyle statusBarStyle; // default is UIStatusBarStyleDefault - (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated; | 设置状态栏样式(UIStatusBarStyleDefault/UIStatusBarStyleLightContent)。 |
@interface ViewController ()
@property (nonatomic) BOOL statusBarIsHidden;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.statusBarIsHidden = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
[self performSelector:@selector(setStatusBarHidden:) withObject:@(YES) afterDelay:3.];
[self performSelector:@selector(setStatusBarHidden:) withObject:@(NO) afterDelay:6.];
}
- (void)setStatusBarHidden:(BOOL)hidden
{
self.statusBarIsHidden = hidden;
[UIView animateWithDuration:0.4
animations:^{
[self setNeedsStatusBarAppearanceUpdate];
}];
[[UIApplication sharedApplication] setStatusBarHidden:hidden withAnimation:UIStatusBarAnimationSlide];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
- (BOOL)prefersStatusBarHidden
{
return self.statusBarIsHidden;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationSlide;
}
@end
七、自定义状态栏
见参考资料:
在状态栏上做渐变动画效果
定制iOS 7中的导航栏和状态栏
遮挡iPhone系统栏实现自定义状态栏的代码
把UIView覆盖到状态栏上的方法