Disabling user interaction of the current view on screen

25,906

Solution 1

Maybe you want the whole application to not react at all?

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

use [[UIApplication sharedApplication] endIgnoringInteractionEvents]; to revert this (credits to nerith)

same for Swift:

UIApplication.sharedApplication().beginIgnoringInteractionEvents()
UIApplication.sharedApplication().endIgnoringInteractionEvents()

and Swift 3/4

UIApplication.shared.beginIgnoringInteractionEvents()
UIApplication.shared.endIgnoringInteractionEvents()

edit for iOS 13: beginIgnoringInteractionEvents is deprecated in iOS13

just make a new full size View and lay it over your current view. that will allow you to block any user interaction.

Solution 2

Here is the code for Swift 3

UIApplication.shared.beginIgnoringInteractionEvents() 
UIApplication.shared.endIgnoringInteractionEvents()

Slight update to the syntax

Solution 3

I've done something very similar to this. I disable all user interaction by placing a translucent black view over everything else, which visually distinguishes the fact that the entire UI is disabled, and blocks all touch events. I usually just add this view to the window class after I've added the view controller's view to the window, and then just hide it when it's not needed.

Solution 4

I am a little hesitant to disable interactions for the entire app - this is too aggressive and too intrusive, what happens when the view controller is inside a split view controller? Then both view controllers will be disabled!

Instead, you could create a clear-colored view controller and presented it modally, see example below for Swift 2:

private func TransparentViewController() -> UIViewController {
  let transparentViewController = UIViewController(nibName: nil, bundle: nil)
  transparentViewController.modalPresentationStyle = .OverCurrentContext
  transparentViewController.modalTransitionStyle = .CrossDissolve
  transparentViewController.view.backgroundColor = UIColor.clearColor()
  return transparentViewController
}

And now you can present it from within your view controller, before presenting the HUD:

let transparentViewController = TransparentViewController()
self.presentViewController(transparentViewController, animated:false, completion: nil) 

Hope this helps!

Solution 5

Use following code for disabling interaction with background

//Ignore interaction for background activities
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

Now if you want to enable the interaction use following snippet

if ([[UIApplication sharedApplication] isIgnoringInteractionEvents]) {

    // Start interaction with application
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
Share:
25,906
Vin
Author by

Vin

Professional Objective C developer with a passion for iOS.

Updated on August 04, 2020

Comments

  • Vin
    Vin over 3 years

    My app has many views and their respective controllers. Now I have a set of model classes with business logic in them. One of the model classes(subclass of NSObject) has the responsibility of managing security. It's intended function is to listen for a particular instruction from a web server and if a 'disable' message arrives from server, disable the UI for any further usage.

    Now the 'disable' message can arrive at any instant during the functioning of the app and any view can be visible on screen. How do I determine which view is visible to the user(from my model class) and disable user interaction for it?

  • Vin
    Vin over 12 years
    cwieland thanks for your answer. I thought of this way, but it would require lot of similar code to be written(I have around 30 views and view controllers). However +1 for the idea of subclassing UIViewController, I would have done that if my app was not already 90% complete.
  • Vin
    Vin over 12 years
    Thanks for your reply Micah. An overlay was the first thing that came to my mind. But since I am already using overlays(for network activity etc.) I didn't want extra view to be managed just for this activity.
  • Rik Smith-Unna
    Rik Smith-Unna over 11 years
    Also UIViews added to the mainwindow don't respond to orientation changes
  • Deamon
    Deamon about 9 years
    Didn't know this exists until now. Very cool! Had been disabling userInteraction on window, which produces some unexpected nil error.
  • Bruce
    Bruce over 8 years
    I had been using userInteractionEnabled = false everywhere, for example navigation bar when progress hud is shown.. should have known this earlier!
  • sabiland
    sabiland over 7 years
    Omg didn't know about this - great!
  • Martin Kovachev
    Martin Kovachev over 7 years
    Just note that this method works in a 'cascade' fashion. If you call 'begin' 10 times - you have to also call 'end' 10 times - which can be very nice or can be a head banger if you didn't know it ;)
  • onmyway133
    onmyway133 almost 7 years
    Most of the time, you want UIApplication.shared.keyWindow?.isUserInteractionEnabled = false
  • iOS dev
    iOS dev over 4 years
    Deprecated from iOS13.0
  • Gotschi
    Gotschi over 4 years
    @iOSdev is there a replacement for this?