Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a UIPopoverController be moved around the screen?

Is it possible to move a UIPopovercontroller around the screen once its already been presented?
I have a small view in my app that can move slightly, and I would like a UIPopoverController to move around with it without having to re-present the UIPopoverController every time it moves. Is this possible?

like image 405
SSS Avatar asked Nov 05 '22 22:11

SSS


1 Answers

As of iOS 9, "UIPopoverController is deprecated. Popovers are now implemented as UIViewController presentations. Use a modal presentation style of UIModalPresentationPopover and UIPopoverPresentationController."

Use the following to present a popover. You may be able to change the values of sourceRect and sourceView but this isn't intended API behavior.

    DetailViewController *detailVC = self.detailViewController;
    detailVC.preferredContentSize = CGSizeMake(420, 92);
    detailVC.modalPresentationStyle = UIModalPresentationPopover;


    UIPopoverPresentationController *presController = detailVC.popoverPresentationController;
    if (presController) {
        presController.delegate = detailVC;
        presController.barButtonItem = self.detailButton;
        presController.sourceRect = self.view.frame;
        presController.sourceView = self.view;
        presController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    }

    [self presentViewController: detailVC animated:YES completion:^{

    }];

Instead, I suggest keeping a reference to the view controller presented with the popover, dismiss the popover and re-present.

DetailViewController *detailVC = self.detailViewController;
[self dismissViewControllerAnimated:NO completion:^{

    UIPopoverPresentationController *presController = detailVC.popoverPresentationController;
    if (presController) {
        presController.delegate = detailVC;
        presController.barButtonItem = self.detailButton;
        presController.sourceRect = self.view.frame; //Update frame
        presController.sourceView = self.view;
        presController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    }

    [self presentViewController: detailVC animated:NO completion:^{

    }];
}];
like image 86
W Dyson Avatar answered Nov 09 '22 06:11

W Dyson