Disabled touch when UIProgressBarHUD is shown

我只是一个虾纸丫 提交于 2019-12-25 01:54:30

问题


I've created an app where I'm creating a UIProgressBarHUD to show that something is loading. My question is, how can I disable the view so nothing can be pressed untill the loading is finished?

I've tried setting:

[self.view setUserInterationEnabled:NO];

However this doesn't work :/

Here is the code I'm using for adding the UIProgressHUD:

- (IBAction) showHUD:(id)sender
{
       //[self.view setUserInteractionEnabled:NO];

       UIProgressHUD *HUD = [[UIProgressHUD alloc]
initWithWindow:[[UIApplication sharedApplication] keyWindow]];
       [HUD setText:@"Loading…"];
       [HUD show:YES];

       [HUD performSelector:@selector(done) withObject:nil afterDelay:1.5];
       [HUD performSelector:@selector(setText:) withObject:@"Done!"
afterDelay:1.5];
       [HUD performSelector:@selector(hide) withObject:nil afterDelay:4.0];

       //[self.view setUserInteractionEnabled:YES];

       [HUD release];
}

Any help would be muchly appreciated!! - James


回答1:


As pointed out here, UIProgressHUD is private. You should not use it.

There is a library that gives you what you are looking for though.

It allows you to keep the user from tapping anything while it is updating as you requested.




回答2:


You can disable user interaction with the nifty property named userInteractionsEnabled, that is defined for UIView. It just so happens that UIWindow is a subclass of UIView, we we can easily disable user interactions for out whole app.

anyViewInYouApp.window.userInteractionsEnabled = NO;

Or keep a reference to the window if you like.




回答3:


In MBProgressHUD.m

#define APPDELEGATE                         
(TMAppDelegate *)[[UIApplication sharedApplication]delegate]

- (void)show:(BOOL)animated 
{
    [[APPDELEGATE window] setUserInteractionEnabled:NO]; //use this line
}

- (void)hide:(BOOL)animated 
{  
    [[APPDELEGATE window] setUserInteractionEnabled:YES]; //use this line
}



回答4:


UIWindow *win = [UIApplication sharedApplication].keyWindow;
[win setUserInteractionEnabled:NO];


来源:https://stackoverflow.com/questions/1504806/disabled-touch-when-uiprogressbarhud-is-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!