sammcewan / WYPopoverController

WYPopoverController is for the presentation of content in popover on iPhone / iPad devices. Very customizable.
Other
253 stars 74 forks source link

How do i reposition a popover after rotation? #34

Open maciekish opened 9 years ago

maciekish commented 9 years ago

How does one actually use -popoverController:willRepositionPopoverToRect:inView:?

I tried the following without luck:

- (void)popoverController:(WYPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view
{
    DLog(@"Present position %f %f %f %f", rect->origin.x, rect->origin.y, rect->size.width, rect->size.height);

    rect->origin.x = 100.f;
    rect->origin.y = 100.f;

    DLog(@"New position %f %f %f %f", rect->origin.x, rect->origin.y, rect->size.width, rect->size.height);
}

The output from this is:

Present position 0.000000 0.000000 0.000000 0.000000
New position 100.000000 100.000000 0.000000 0.000000

However the popover origin still appears to be 0,0,0,0 after rotation.

tomhamming commented 9 years ago

Has anyone done this successfully? I think the delegate is supposed to set the rect and view, but it's not clear to me how to determine that. In my case, I want it to keep pointing at the same view onscreen after rotation.

tomhamming commented 9 years ago

Got it. If the popover is presented from a bar button, it just uses the bounds and the bar button's view as its rect and view. If you're presenting a popover pointing at a view, you probably did:

[popover presentPopoverFromRect:view.frame inView:view.superview permittedArrowDirections:dir  animated:YES];

Instead do this:

[popover presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:dir animated:YES];

Remember the view you presented from, and give the delegate method the view's bounds and the view. I've got a singleton class that I use to coordinate popovers (dismiss the active one, etc), and I have a shortcut method on that class that looks like this:

-(WYPopoverController *)presentPopoverFromView:(UIView *)view withContent:(UIViewController *)content;

The singleton keeps track of the view it was given and handles the rotation.

tomhamming commented 9 years ago

It would be cool if the popover had a presentPopoverFromView method that handled rotation automatically the same way it does for the bar button item...