Here is a code fragment from one of my projects. Basically, if the popover is showing, you present the popover again in the method didRotateFromInterfaceOrientation:
, which is sent to the view controller after the user interface rotation has taken place. (The willRotate...
and willAnimateRotation...
methods are called before the rotation has taken place, so it is the wrong place for the presentPopover...
method call.)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// if the popover is showing, adjust its position after the re-orientation by presenting it again:
if (self.myPopoverController != nil) // if the popover is showing (replace with your own test if you wish)
{
[self.myPopoverController presentPopoverFromRect:attachmentRect
inView:myView
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
}
In the above, self.myPopoverController
is a property of my view controller where I store a reference to the popover when it is created. When I dismiss and discard the popover under normal circumstances, I take care to set this property to nil
, so I can check it for 'non-nil
'ness to decide whether or not the popover is being shown.
Note, however, that you don't need to dismiss the popover before the rotation takes place. Just present the same popover again. (This is where keeping a reference to the popover comes in handy.)
In your case, where the popover emanates from a toolbar button, you would use something like the following instead:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// if the popover is showing, adjust its position after the re-orientation by presenting it again:
if (self.myPopoverController != nil) // if the popover is showing (replace with your own test if you wish)
{
[self.myPopoverController presentPopoverFromBarButtonItem:barButtonItem
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}