BadmintonCat
BadmintonCat

Reputation: 9586

Repositioning UIPopoverController on device rotation

I'm trying to re-position a popover view when the device is rotated. I use this code to create the popover:

        settingsViewCtrl = storyboard.instantiateViewControllerWithIdentifier("settingsViewCtrl") as SettingsViewCtrl;
        settingsPopoverCtrl = UIPopoverController(contentViewController: settingsViewCtrl);
        settingsPopoverCtrl.delegate = self;
        let m:CGFloat = min(view.frame.width, view.frame.height);
        let s:CGSize = CGSizeMake(m - 100, m - 100);
        let r:CGRect = CGRectMake(view.frame.width * 0.5, view.frame.height * 0.5, 1, 1);
        settingsPopoverCtrl.setPopoverContentSize(s, animated: true);
        settingsPopoverCtrl.presentPopoverFromRect(r, inView: view, permittedArrowDirections: nil, animated: true);

And I use willRepositionPopoverToRect in my delegate ...

func popoverController(popoverController:UIPopoverController!, willRepositionPopoverToRect rect:UnsafeMutablePointer<CGRect>, inView view:AutoreleasingUnsafeMutablePointer<UIView?>)
{
    if (settingsPopoverCtrl != nil && settingsViewCtrl != nil)
    {
        let r:CGRect = CGRectMake(self.view.frame.width * 0.5, self.view.frame.height * 0.5, 1, 1);
        settingsPopoverCtrl.presentPopoverFromRect(r, inView: self.view, permittedArrowDirections: nil, animated: true);
    }
}

But that can't be it since when I rotate the device I'm getting the warning: Application tried to represent an active popover presentation: and the popover also isn't repositioned.

How do I get to apply and update positioning CGRect in willRepositionPopoverToRect so that the popover view updates? Would it be possible to modify the provided rect:UnsafeMutablePointer<CGRect> and give it back to the popover?


UPDATE:

According to the docs (https://developer.apple.com/library/ios/documentation/uikit/reference/UIPopoverControllerDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIPopoverControllerDelegate/popoverController%3awillRepositionPopoverToRect%3ainView%3a):

If you want to propose a different rectangle for the popover, put the new value in this parameter.

So the question is: How do I put the new value into this rect?

Upvotes: 3

Views: 2047

Answers (1)

BadmintonCat
BadmintonCat

Reputation: 9586

Let me answer this:

        let r:CGRect = CGRectMake(self.view.frame.width * 0.5, self.view.frame.height * 0.5, 1, 1);
        rect.initialize(r);

... will obviously init the given rect with new dimensions and it works flawless when changing device rotation, unlike the misleading code on Apple's docs where they tell to use presentPopoverFromRect once again, which totally doesn't work.

Upvotes: 5

Related Questions