通过UIWindow创建状态栏的通知





//
//  HQStatusBarHUD.m
//  StatusBarHUD
//
//  Created by hq on 16/5/3.
//  Copyright © 2016年 hanqing. All rights reserved.
//

#import "HQStatusBarHUD.h"

#define  HQScreenWidth [UIScreen mainScreen].bounds.size.width

//提示框的高度
#define HQTipsHeight 40

//字体大小
#define HQFontSize [UIFont systemFontOfSize:14]

@interface HQStatusBarHUD()

@end

@implementation HQStatusBarHUD


static UIWindow *window;

static NSTimer *timer;


+(void) setUpWindow{
    
    window=[[UIWindow alloc] init];
    
    window.windowLevel=UIWindowLevelStatusBar;
    
    window.frame=CGRectMake(0, -HQTipsHeight, HQScreenWidth,HQTipsHeight);
    
    window.hidden=NO;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        window.frame=CGRectMake(0,0, HQScreenWidth,HQTipsHeight);
        
    }];
}


+(void) showMessage:(NSString *) message image:(UIImage *) image messageBgColor:(UIColor *)bgColor {
    
    [timer invalidate];
    
    [self setUpWindow];
    
    window.backgroundColor=bgColor;
    

    UIButton *but=[[UIButton alloc]initWithFrame:window.bounds];
    
    but.titleEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0);
    
    [but setTitle:message forState:UIControlStateNormal];
    
    [but setImage:image forState:UIControlStateNormal];
    
    but.titleLabel.textColor=[UIColor whiteColor];
    
    but.titleLabel.font=HQFontSize;
    
    [window addSubview:but];
    
    //添加自动消失动画
    timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(hide) userInfo:nil repeats:NO];

}

//显示成功信息
+(void) showSuccessWithString:(NSString *) message{

    [self showMessage:message image:[UIImage imageNamed:@"StatusBarHUD.bundle/success_ico"] messageBgColor:[UIColor colorWithRed:0.600 green:0.800 blue:0.400 alpha:1.000]];
}

+(void) showErrorWithString:(NSString *) message{
    
    [self showMessage:message image:[UIImage imageNamed:@"StatusBarHUD.bundle/error_icon"] messageBgColor:[UIColor colorWithRed:1.000 green:0.216 blue:0.246 alpha:1.000]];
}

+(void) showMessageWithString:(NSString *) message{
    
    [self showMessage:message image:[UIImage imageNamed:@"StatusBarHUD.bundle/info_icon"] messageBgColor:[UIColor colorWithRed:1.000 green:0.600 blue:0.000 alpha:1.000]];
}

+(void)showLoading:(NSString *)message messageBgColor:(UIColor *)bgColor{
    
    [self setUpWindow];
    
    window.backgroundColor=bgColor;
    
    UIActivityIndicatorView *indicator=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    
    indicator.frame=CGRectMake(0, (HQTipsHeight-indicator.bounds.size.height)*0.5, indicator.bounds.size.width, indicator.bounds.size.height);
    
    [indicator startAnimating];
    
    CGFloat labW=[message sizeWithAttributes:@{NSFontAttributeName:HQFontSize}].width;
    
    UILabel *lab=[[UILabel alloc]init];
    
    lab.frame=CGRectMake(indicator.bounds.size.width, 0, labW, HQTipsHeight);
    
    lab.text=message;
    
    lab.textAlignment=NSTextAlignmentCenter;
    
    lab.textColor=[UIColor whiteColor];
    
    lab.font=HQFontSize;

    
    UIView *view=[[UIView alloc] init];
    
    view.bounds=CGRectMake(0, 0, indicator.bounds.size.width+labW, HQTipsHeight);
    
    view.center=window.center;
    
    
    [view addSubview:indicator];
    
    [view addSubview:lab];

    
    [window addSubview:view];
}

+(void) showLoadingWithString:(NSString *) message{
    
    [self showLoading:message messageBgColor:[UIColor colorWithRed:1.000 green:0.600 blue:0.000 alpha:1.000]];
}

+(void) hide{
    
    [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        window.frame=CGRectMake(0,-HQTipsHeight, HQScreenWidth,HQTipsHeight);
    } completion:^(BOOL finished) {
        window=nil;
    }];
}


@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值