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?
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:^{
    }];
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With