25
votes

How can I disable all Input while the UIActivityIndicatorView is spinning?

Thanks

4
If you add the spinner to a UIAlertView and then show the alert, then this will achieve what you are after. - Luke
You can also achieve a nice effect with MBProgressHUD but that may be heavier the you want depending on what you are trying to achieve. - FluffulousChimp
thanks Luke, how do I "destroy" the UIAlertView if I want it to disapear. Is it OK to have 0 buttons? - mica
It's perfectly fine to not have buttons. Just call [someAlertView dismissWithClickedButtonIndex:0 animated:YES]; - Brandon Schlenker
Please go through this Link [Has Detailed Conversation about this Topic][1] [1]: stackoverflow.com/questions/5404856/… - Kamar Shad

4 Answers

53
votes

You can call beginIgnoringInteractionEvents when you start the spinner

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

and endIgnoringInteractionEvents when you stop it.

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

Just make sure your code always comes to the point where you call endIgnoringInteractionEvents, otherwise your app will freeze (from the users point of view).

8
votes

In Swift 3.0:

To disable interaction:

UIApplication.shared.beginIgnoringInteractionEvents() 

To restore interaction:

UIApplication.shared.endIgnoringInteractionEvents() 
1
votes

Just an addition to rokjarc answer. Here an example of watchdog to keep app alive. You can call always with some critical interval, maybe 10 sec. And if you need to enable within 10 sec, just call "enable" method.

UIWindow * __weak mainWindow;

- (void)disableGlobalUserInteractionForTimeInterval:(NSTimeInterval)interval
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mainWindow = [[[UIApplication sharedApplication] windows] lastObject];
    });

    [mainWindow setUserInteractionEnabled:false];

    if (interval > 0)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self enableGlobalUserInteraction];
        });
    }
}

- (void)enableGlobalUserInteraction
{
    if (mainWindow)
    {
        [mainWindow setUserInteractionEnabled:true];
    }
}
0
votes

In Swift 5:

// activity indicator starts
view.isUserInteractionEnabled = false

...

// activity indicator stops
view.isUserInteractionDisabled = true