您的位置:首页 > Web前端 > CSS

ios MBProgressHUD使用以及自定义样式

2017-03-02 16:20 841 查看
使用第三方的工具类 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 *imag
4000
e = [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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐