Domanda

I have a popover that displays from a UITableViewCell in a UITableView, which is the only view in a modal dialog. This works fine in both landscape and portrait:

UIViewController *content = [[SetTimeClockTimeViewController alloc] init];
popover = [[UIPopoverController alloc] initWithContentViewController:content];
[popover setDelegate:self];
UITableViewCell *cell = [dataTable cellForRowAtIndexPath:[dataTable indexPathForSelectedRow]];
[popover presentPopoverFromRect:cell.frame inView:cell.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

However, when the screen is rotated while the popover remains up, the popover shifts away from the cell (if I rotate back it shifts to the proper location). This happens if either orientation is the starting orientation. I tried implementing popoverController:willRepositionPopoverToRect:inView:, but nothing I've put in it appears to have fixed the problem. For example:

- (void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view {
    // TODO: Popover moves wrong when rotating
    UITableViewCell *cell = [dataTable cellForRowAtIndexPath:[dataTable indexPathForSelectedRow]];
    *rect = cell.frame;
    *view = cell.superview;
}

I've tried telling the table to reloadData when rotating, using convertRect:toView to have self.view as the view, and calling presentPopoverFromRect:inView:permittedArrowDirections:animated in didRotateFromInterfaceOrientation:, but none of those seemed to fix the improper placement.

How can I ensure the popover is displayed from the new cell location after rotation?

Example of the popover displaying in portrait (arrow points to "Time" row): Example of the popover displaying in portrait

After orientation to landscape (arrow should point to "Time" row): After orientation to landscape

È stato utile?

Soluzione

Popovers displayed from a view do not maintain a relationship with the view, and might more often than not shift to incorrect places after rotation. This is unlike displaying from a bar button item, where the popover will move after layout. You should move the popover to the correct location in viewDidLayoutSubviews of the view controller.

Make sure to convert the returned rectangle to the view's correct coordinate system by using convertRect:toView: or convertRect:fromView:.

Altri suggerimenti

You should translate the rect to the superview using convertRect:toView. The most important part is to call it in willAnimateToInterfaceOrientation method.

The best solution I have seen for this is to use the cell(or in my case a UIButton) as the sourceView and the cell's bounds as the sourceRect. i.e.

[popover 
   presentPopoverFromRect:cell.bounds //vs cell.frame
   inView:cell                        //vs cell.superView
   permittedArrowDirections:UIPopoverArrowDirectionAny 
   animated:YES];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top