ios MBProgressHUD使用以及自定义样式

来源:互联网 发布:拳霸摇杆知乎 编辑:程序博客网 时间:2024/05/09 18:52

使用第三方的工具类 MBProgressHUD


实现方式如下:

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

 hud.mode = MBProgressHUDModeAnnularDeterminate;

 hud.labelText = @"正在加载中。。。";


实现自定义视图的提示

#pragma  mark - 添加提示框
-(void)addHud:(NSString *)text :(UIImage *)img{
    
    MBProgressHUD *  HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.mode = MBProgressHUDModeCustomView;//自定义视图模式
    
    UIImage *image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];//可以选择不同方式加载图片
    HUD.customView = [[UIImageView alloc] initWithImage:image];
    HUD.square = YES;//不知道干嘛用的

    HUD.labelText =text;
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [HUD removeFromSuperview];
        
    }];
    

}


一个项目中可能要在多个页面使用,将方法提出来,写成一个公共方法

扩展mbprogresshub

在.h中
#import "MBProgressHUD.h"

@interface MBProgressHUD(Add)

+(void)showErrorWithImg:(NSString *)error :(UIImage *)img toView:(UIView *)view;

+(void)showSucessWithImg:(NSString *)sucess :(UIImage *)img toView:(UIView *)view;

@end

在.m中
#import "HubTipShow.h"
#import "MBProgressHUD.h"

@implementation MBProgressHUD(Add)

+(void)showErrorWithImg:(NSString *)error :(UIImage *)img toView:(UIView *)view{
    MBProgressHUD *  HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    HUD.mode = MBProgressHUDModeCustomView;
    
    UIImage *image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    HUD.customView = [[UIImageView alloc] initWithImage:image];
    HUD.square = YES;
    
    HUD.labelText = error;
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [HUD removeFromSuperview];
        
    }];
    
}

+(void)showSucessWithImg:(NSString *)sucess :(UIImage *)img toView:(UIView *)view{
    MBProgressHUD *  HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    HUD.mode = MBProgressHUDModeCustomView;
    
    UIImage *image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    HUD.customView = [[UIImageView alloc] initWithImage:image];
    HUD.square = YES;
    
    HUD.labelText = sucess;
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [HUD removeFromSuperview];
        
    }];
}

@end

在controller中,一句话即可解决

        [MBProgressHUD showErrorWithImg:@"账号不能为空" :[UIImage imageNamed:@"errormark"] toView:self.view];
        [MBProgressHUD showSucessWithImg:@“登录成功”:[UIImage imageNamed:@"Checkmark"] toView:self.view];



0 0