Disabling user interaction of the current view on screen

前端 未结 7 629
一个人的身影
一个人的身影 2021-01-31 02:52

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 res

相关标签:
7条回答
  • 2021-01-31 03:31

    Here is the code for Swift 2.2 iOS 9.3

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

    Use it frequently, work like champ for me, very useful for a view with many IBActions that make API call and you dont want to make another call in waiting for first response

    0 讨论(0)
  • 2021-01-31 03:35

    You could add a delegate in the class that is listening to the server and so when it gets that message it just calls disable on whomever its delegate is. Whichever view is showing to get the message as well as normal execution until the message is received. If it is a singleton just set the view as the delegate on viewWillAppear.

    Another viable option is to use the notification center. So when your class gets the disable message just do

    [[NSNotificationCenter defaultCenter] postNotificationName:@"disableView" object:nil];
    

    and when your views load add them to listen

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disableView:) name:@"disableView" object:nil];
    

    Then stop listening when they aren't needed.

    Subclassing UIViewController and implementing the disable functionality and then subclassing that class in all other view controllers would eliminate the duplication of code.

    0 讨论(0)
  • 2021-01-31 03:38

    Here is the code for Swift 3

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

    Slight update to the syntax

    0 讨论(0)
  • 2021-01-31 03:38

    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];
    }
    
    0 讨论(0)
  • 2021-01-31 03:48

    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!

    0 讨论(0)
  • 2021-01-31 03:52

    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.

    0 讨论(0)
提交回复
热议问题