IOS狀態(tài)欄自定義提示后消失

1.自定義狀態(tài)欄.h文件
////  DGCustomStatusBar.h//  狀態(tài)欄提示之后消失////  Created by naton on 15/11/11.//  Copyright ? 2015年 zqk. All rights reserved.//
#import@interface DGCustomStatusBar : UIWindow
@property (nonatomic, strong)UIColor *statusColor;
@property (nonatomic, strong)UIColor *textColor;
@property (nonatomic, assign)NSTextAlignment textAlignment;
@property (nonatomic, strong)UIFont *textFont;
- (void)showStatusWithMessage:(NSString *)text;
@end
2.自定義狀態(tài)欄.m文件
//
//  DGCustomStatusBar.m
//  狀態(tài)欄提示之后消失
//
//  Created by naton on 15/11/11.
//  Copyright ? 2015年 zqk. All rights reserved.
//
#import "DGCustomStatusBar.h"
@interface DGCustomStatusBar ()
@property (nonatomic, strong)UILabel *label;
@end
@implementation DGCustomStatusBar
- (instancetype)init {
self = [super init];
if (self) {
self.frame = [UIApplication sharedApplication].statusBarFrame;
//UIWindow 有三個層級,分別是Normal ,StatusBar,Alert.輸出他們?nèi)齻€層級的值,我們發(fā)現(xiàn)從左到右依次是0,1000,2000
//設(shè)置window的顯示層級高于UIWindowLevelStatusBar.
self.windowLevel = UIWindowLevelStatusBar + 1.0f;
self.backgroundColor = [UIColor blackColor];
self.userInteractionEnabled = NO;
self.alpha = 0;
[self createLabel];
//makeKeyAndVisible不會使window的引用計數(shù)+1,所以在使用的時候一定要將window設(shè)置成全部變量,如果是個局部變量window在執(zhí)行完makeKeyAndVisible方法之后會被釋放,不會顯示出來.
[self makeKeyAndVisible];
}
return self;
}
- (void)createLabel {
_label = [[UILabel alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentRight;
[self addSubview:_label];
}
- (void)showStatusWithMessage:(NSString *)text {
_label.text = text;
if (self.alpha == 1) {
//當(dāng)DGCustomStatusBar已經(jīng)顯示出來了,再連續(xù)點擊顯示按鈕,取消延時執(zhí)行,不讓window隱藏.
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideWindow:) object:nil];
}
[UIView animateWithDuration:1.0f animations:^{
self.alpha = 1;
} completion:^(BOOL finished) {
[self performSelector:@selector(hideWindow:) withObject:nil afterDelay:1.0f];
}];
}
- (void)hideWindow:(id)object
{
[UIView animateWithDuration:1.0f animations:^{
self.alpha = 0;
}];
}
- (void)setStatusColor:(UIColor *)statusColor
{
_statusColor  =statusColor;
self.backgroundColor = statusColor;
}
- (void)setTextColor:(UIColor *)textColor
{
_textColor = textColor;
_label.textColor = textColor;
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
_textAlignment = textAlignment;
_label.textAlignment = textAlignment;
}
- (void)setTextFont:(UIFont *)textFont
{
_textFont = textFont;
_label.font = textFont;
}
@end
3.具體使用
#import "ViewController.h"
#import "DGCustomStatusBar.h"
@interface ViewController ()
@property (nonatomic, strong)DGCustomStatusBar *statusBar;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btn:(id)sender {
[self.statusBar setBackgroundColor:[UIColor redColor]];
[self.statusBar setTextColor:[UIColor whiteColor]];
[self.statusBar setTextAlignment:NSTextAlignmentCenter];
[self.statusBar showStatusWithMessage:@"分享成功"];
}
- (DGCustomStatusBar *)statusBar {
if (_statusBar == nil) {
_statusBar = [[DGCustomStatusBar alloc] init];
}
return _statusBar;
}
@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容