自定義iOS狀態(tài)欄
作者:佚名
為了讓自定義的狀態(tài)欄可以讓用戶看到,設置了它的windowlevel,在ios中,windowlevel屬性決定了UIWindow的顯示層次,默認的windowlevel為UIWindowLevelNormal,即0.0 。為了能覆蓋默認的狀態(tài)欄,將windowlevel設置高點。其他代碼基本上都不解釋什么,如果要特殊效果,可以自己添加。
如果需要在狀態(tài)欄顯示自定義的消息時,就需要自定義狀態(tài)欄。
代碼如下:
XYCustomStatusBar.h
- #import <UIKit/UIKit.h>
- @interface XYCustomStatusBar : UIWindow{
- UILabel *_messageLabel;
- }
- - (void)showStatusMessage:(NSString *)message;
- - (void)hide;
- @end
XYCustomStatusBar.m
- #import "XYCustomStatusBar.h"
- @implementation XYCustomStatusBar
- - (void)dealloc{
- [super dealloc];
- [_messageLabel release], _messageLabel = nil;
- }
- - (id)init{
- self = [super init];
- if (self) {
- self.frame = [UIApplication sharedApplication].statusBarFrame;
- self.backgroundColor = [UIColor blackColor];
- self.windowLevel = UIWindowLevelStatusBar + 1.0f;
- _messageLabel = [[UILabel alloc] initWithFrame:self.bounds];
- [_messageLabel setTextColor:[UIColor whiteColor]];
- [_messageLabel setTextAlignment:NSTextAlignmentRight];
- [_messageLabel setBackgroundColor:[UIColor clearColor]];
- [self addSubview:_messageLabel];
- }
- return self;
- }
- - (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 };
- [UIView animateWithDuration:0.5 animations:^{
- self.frame = (CGRect){self.frame.origin, totalSize };
- } completion:^(BOOL finished){
- _messageLabel.text = message;
- }];
- }
- - (void)hide{
- self.alpha = 1.0f;
- [UIView animateWithDuration:0.5f animations:^{
- self.alpha = 0.0f;
- } completion:^(BOOL finished){
- _messageLabel.text = @"";
- self.hidden = YES;
- }];
- }
- @end
為了讓自定義的狀態(tài)欄可以讓用戶看到,設置了它的windowlevel,在ios中,windowlevel屬性決定了UIWindow的顯示層次,默認的windowlevel為UIWindowLevelNormal,即0.0 。為了能覆蓋默認的狀態(tài)欄,將windowlevel設置高點。其他代碼基本上都不解釋什么,如果要特殊效果,可以自己添加。
責任編輯:閆佳明
來源:
oschina