MBProgressHUD在实际项目中用的非常多,不过要处理提示框的地方又要写很多的代码,下面分享一个别人写的方法
直接移植到项目中就可以用了,OC和swift项目中都通用,使用之前把MBProgressHUD库移植到项目中
MBProgressHUD地址:https://github.com/jdg/MBProgressHUD
UIViewController+HUD.h
/************************************************************
* * EaseMob CONFIDENTIAL
* __________________
* Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of EaseMob Technologies.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from EaseMob Technologies.
*/
#import <UIKit/UIKit.h>
@interface UIViewController (HUD)
- (void)showHudInView:(UIView *)view hint:(NSString *)hint;
- (void)hideHud;
- (void)showHint:(NSString *)hint;
// 从默认(showHint:)显示的位置再往上(下)yOffset
- (void)showHint:(NSString *)hint yOffset:(float)yOffset;
@end
UIViewController+HUD.m
/************************************************************
* * EaseMob CONFIDENTIAL
* __________________
* Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of EaseMob Technologies.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from EaseMob Technologies.
*/
#import "UIViewController+HUD.h"
#import "MBProgressHUD.h"
#import <objc/runtime.h>
static const void *HttpRequestHUDKey = &HttpRequestHUDKey;
@implementation UIViewController (HUD)
- (MBProgressHUD *)HUD{
return objc_getAssociatedObject(self, HttpRequestHUDKey);
}
- (void)setHUD:(MBProgressHUD *)HUD{
objc_setAssociatedObject(self, HttpRequestHUDKey, HUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)showHudInView:(UIView *)view hint:(NSString *)hint{
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
HUD.label.text = hint;
[view addSubview:HUD];
[HUD showAnimated:YES];
[self setHUD:HUD];
}
- (void)showHint:(NSString *)hint
{
//显示提示信息
UIView *view = [[UIApplication sharedApplication].delegate window];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.userInteractionEnabled = NO;
// Configure for text only and offset down
hud.mode = MBProgressHUDModeText;
hud.label.text = hint;
hud.margin = 10.f;
CGPoint point = hud.offset;
point.y =IS_IPHONE_5?200.f:150.f;
hud.offset = point;
hud.removeFromSuperViewOnHide = YES;
[hud hideAnimated:YES afterDelay:2];
}
- (void)showHint:(NSString *)hint yOffset:(float)yOffset {
//显示提示信息
UIView *view = [[UIApplication sharedApplication].delegate window];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.userInteractionEnabled = NO;
// Configure for text only and offset down
hud.mode = MBProgressHUDModeText;
hud.label.text = hint;
hud.margin = 10.f;
CGPoint point = hud.offset;
point.y =IS_IPHONE_5?200.f:150.f;
point.y += yOffset;
hud.offset = point;
hud.removeFromSuperViewOnHide = YES;
[hud hideAnimated:YES afterDelay:2];
}
- (void)hideHud{
[[self HUD] hideAnimated:YES];
}
@end