最近使用了开源类MBProgressHUD。有几个值得总结的地方!
(1)初始化MBProgressHUD
方法一:
- (IBAction)showWithLabel:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; [self.navigationController.view addSubview:HUD]; HUD.delegate = self; HUD.labelText = @"Loading"; [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES]; }
请看代码:
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; [self.navigationController.view addSubview:HUD];
方法二:
- (IBAction)showOnWindow:(id)sender { // The hud will dispable all input on the window HUD = [[MBProgressHUD alloc] initWithView:self.view.window]; [self.view.window addSubview:HUD]; HUD.delegate = self;HUD.labelText = @"Loading";[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];}
请看代码:
HUD = [[MBProgressHUD alloc] initWithView:self.view.window]; [self.view.window addSubview:HUD];
当隐藏状态栏时,方法二会导致状态栏再次出现时和视图重叠。而换用方法一则是正常的。
备注:上面的代码来自“MBProgressHUD-master.zip”实例工程(下载地址:https://github.com/jdg/MBProgressHUD)。
(2)MBProgressHUD显示时,默认会阻塞它所在的视图上所有控件的活动事件,导致用户只能干等,直到MBProgressHUD消失(隐藏)。
如果我们想在MBProgressHUD显示时,仍然能点击视图上的其他按钮呢?比如,点击“返回”回到前一个视图界面。
有两个方法可以解决这个问题!
方法一:
自定义一个子视图(如:mySubview),专门用于addSubview:HUD。
HUD = [[MBProgressHUD alloc] initWithView:self.mySubview];
[self.mySubview addSubview:HUD];
关于mySubview的初始化省略,需要明确的一点是:
[self.view addSubview:self.mySubview];
备注:方法一还未经过验证。
方法二:
一个简单的方法:
在初始化HUD时,增加下面的语句:
HUD.userInteractionEnabled = NO;
备注:方法二有效!在MBProgressHUD时,视图上控件的活动事件不会被阻塞。
【参考资料】
(1)https://github.com/jdg/MBProgressHUD 【这个是MBProgressHUD代码维护分支】
(2)http://stackoverflow.com/questions/10590847/mbprogresshud-blocks-interactions-with-an-uiscrollview-when-shown
(3)http://stackoverflow.com/tags/mbprogresshud/hot