Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent animation on UIPopover dismissed by tap outside?

When a user taps outside the popover, the dismissal is animated. Is there a way to set that dismissal animation to NO? I have googled and searched on Stack extensively.

The docs for UIPopover state:

When displayed, taps outside of the popover window cause the popover to be dismissed automatically. To allow the user to interact with the specified views and not dismiss the popover, you can assign one or more views to the passthroughViews property. Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismissPopoverAnimated: method as needed.

I have implemented the dismissPopoverAnimated: method with NO and that works great for all the cases when I call that method.

The problem is when a user taps outside the popover to dismiss, dismissPopoverAnimated: is not called.

taps outside of the popover window cause the popover to be dismissed automatically.

And that dismissal is animated. There seems to be no way to control that dismissal. I am using the popover to present a color picker for a drawing app. Taps to draw are not registered until the popover has finished animating out. This creates a noticeable delay as you are not able to draw immediately but must wait for the animation to complete.

I thought that - (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController could work but there is no way AFAIK to set the animation property in this method. Just return yes or no.

Is there a different method I can implement to be able to set the animation to NO?

like image 408
Joshua Dance Avatar asked Jun 19 '13 23:06

Joshua Dance


3 Answers

In the view controller that presents your UIPopoverController, conform to the UIPopoverControllerDelegate protocol and implement the following delegate method. I just tested this and it does dismiss the popover without animation.

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
    [self.myPopoverController dismissPopoverAnimated:NO];
    return YES;
}

Just make sure that you have set the delegate of your popover controller to the view controller that implements this.

like image 60
geraldWilliam Avatar answered Nov 03 '22 05:11

geraldWilliam


Swift 5

This will disable the animation, when we close the popOver by tapping outside.

extension YourViewController: UIPopoverPresentationControllerDelegate {

  func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
        // to prevent animation, we need to dismiss it manuallly with animated: false
        presentationController.presentingViewController.dismiss(animated: false, completion: nil)
        return true
    }
 }
like image 22
Umair Afzal Avatar answered Nov 03 '22 03:11

Umair Afzal


On iOS 9+ as by default modalPresentationStyle = .Popover you can implement this method to prevent dismiss clicking out

public func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return false
    }
like image 33
TheCodeTalker Avatar answered Nov 03 '22 03:11

TheCodeTalker