IOS状态栏自定义提示后消失

1.自定义状态栏.h文件
////  DGCustomStatusBar.h//  状态栏提示之后消失////  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.自定义状态栏.m文件
//
//  DGCustomStatusBar.m
//  状态栏提示之后消失
//
//  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.输出他们三个层级的值,我们发现从左到右依次是0,1000,2000
//设置window的显示层级高于UIWindowLevelStatusBar.
self.windowLevel = UIWindowLevelStatusBar + 1.0f;
self.backgroundColor = [UIColor blackColor];
self.userInteractionEnabled = NO;
self.alpha = 0;
[self createLabel];
//makeKeyAndVisible不会使window的引用计数+1,所以在使用的时候一定要将window设置成全部变量,如果是个局部变量window在执行完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) {
//当DGCustomStatusBar已经显示出来了,再连续点击显示按钮,取消延时执行,不让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

IOS状态栏自定义提示后消失_第1张图片

你可能感兴趣的:(IOS状态栏自定义提示后消失)