iOS7 StatusBar 在需要隐藏或改变样式时在UIViewConroller中调用:
[selfsetNeedsStatusBarAppearanceUpdate];
1、隐藏
StatusBar在iOS7中无法使用一下接口隐藏:
[[UIApplicationsharedApplication]setStatusBarHidden:YES];
若要隐藏需要在UIViewController中实现下列函数:
- (BOOL)prefersStatusBarHidden
{
returnYES;
}
2、样式改变
iOS 7中statusbar 有两种样式:白色字体UIStatusBarStyleLightContent和黑色字体UIStatusBarStyleDefault。
如果改变需要在UIViewController中实现:
- (UIStatusBarStyle)preferredStatusBarStyle
{
returnUIStatusBarStyleDefault;
}
3、自定义状态栏,方法一
UIWindow* statusWindow = [[UIWindow alloc]initWithFrame:[UIApplication sharedApplication].statusBarFrame];
[statusWindows etWindowLevel:UIWindowLevelStatusBar +1];
[statusWindow setBackgroundColor:[UIColor clearColor]];
UILabel* statusLabel = [[UILabel alloc]initWithFrame:statusWindow.bounds];
statusLabel.text=@"RSSI:";
statusLabel.textColor= [UIColorblueColor];
statusLabel.textAlignment= NSTextAlignmentCenter;
statusLabel.backgroundColor= [UIColorblackColor];
[statusWindow addSubview:statusLabel];
[statusWindow makeKeyAndVisible];
4、自定义状态栏,方法二
如果需要在状态栏显示自定义的消息时,就需要自定义状态栏。
代码如下:
XYCustomStatusBar.h
#import
@interfaceXYCustomStatusBar : UIWindow{
UILabel*_messageLabel;
}
- (void)showStatusMessage:(NSString*)message;
- (void)hide;
@end
XYCustomStatusBar.m
#import "XYCustomStatusBar.h"
@implementationXYCustomStatusBar
- (void)dealloc{
[superdealloc];
[_messageLabelrelease], _messageLabel =nil;
}
- (id)init{
self= [superinit];
if(self) {
self.frame= [UIApplicationsharedApplication].statusBarFrame;
self.backgroundColor= [UIColorblackColor];
self.windowLevel= UIWindowLevelStatusBar +1.0f;
_messageLabel = [[UILabelalloc]initWithFrame:self.bounds];
[_messageLabelsetTextColor:[UIColorwhiteColor]];
[_messageLabelsetTextAlignment:NSTextAlignmentRight];
[_messageLabelsetBackgroundColor:[UIColorclearColor]];
[selfaddSubview:_messageLabel];
}
returnself;
}
- (void)showStatusMessage:(NSString*)message{
self.hidden=NO;
self.alpha=1.0f;
_messageLabel.text=@"";
CGSize totalSize =self.frame.size;
self.frame= (CGRect){self.frame.origin,0, totalSize.height};
[UIVie wanimateWithDuration:0.5animations:^{
self.frame= (CGRect){self.frame.origin, totalSize };
}completion:^(BOOLfinished){
_messageLabel.text= message;
}];
}
- (void)hide{
self.alpha=1.0f;
[UIView animateWithDuration:0.5fanimations:^{
self.alpha=0.0f;
}completion:^(BOOLfinished){
_messageLabel.text=@"";
self.hidden=YES;
}];
}
@end
为了让自定义的状态栏可以让用户看到,设置了它的windowlevel,在ios中,windowlevel属性决定了UIWindow的显示层次,默认的windowlevel为UIWindowLevelNormal,即0.0 。为了能覆盖默认的状态栏,将windowlevel设置高点。其他代码基本上都不解释什么,如果要特殊效果,可以自己添加。