SSS
SSS

Reputation: 179

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?

Upvotes: 5

Views: 3426

Answers (4)

W Dyson
W Dyson

Reputation: 4634

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:^{

    }];
}];

Upvotes: 1

EricS
EricS

Reputation: 9768

You can re-call presentPopoverFromRect.

See http://developer.apple.com/library/ios/#qa/qa1694/_index.html

Upvotes: 1

WrightsCS
WrightsCS

Reputation: 50697

You can track the users touches around the screen, and use presentPopoverFromRect: to reshow the popover.

Upvotes: 0

Andrew
Andrew

Reputation: 3892

No you can't. This is no method to do that.

pover presentPopoverFromRect:

was mentioned in the comments, but notice the

present

you cannot represent without redrawing.

Upvotes: 0

Related Questions