Popover with embedded navigation controller doesn't respect size on back nav

Ben Zotto picture Ben Zotto · May 2, 2010 · Viewed 44.9k times · Source

I have a UIPopoverController hosting a UINavigationController, which contains a small hierarchy of view controllers.

I followed the docs and for each view controller, I set the view's popover-context size like so:

[self setContentSizeForViewInPopover:CGSizeMake(320, 500)];

(size different for each controller)

This works as expected as I navigate forward in the hierarchy-- the popover automatically animates size changes to correspond to the pushed controller.

However, when I navigate "Back" through the view stack via the navigation bar's Back button, the popover doesn't change size-- it remains as large as the deepest view reached. This seems broken to me; I'd expect the popover to respect the sizes that are set up as it pops through the view stack.

Am I missing something?

Thanks.

Answer

krasnyk picture krasnyk · Feb 17, 2011

Ok, I was struggling with the same issue. None of the above solutions worked for me pretty nicely, that is why I decided to do a little investigation and find out how this works. This is what I discovered: - When you set the contentSizeForViewInPopover in your view controller it won't be changed by the popover itself - even though popover size may change while navigating to different controller. - When the size of the popover will change while navigating to different controller, while going back, the size of the popover does not restore - Changing size of the popover in viewWillAppear gives very strange animation (when let's say you popController inside the popover) - I'd not recommend it - For me setting the hardcoded size inside the controller would not work at all - my controllers have to be sometimes big sometimes small - controller that will present them have the idea about the size though

A solution for all that pain is as follows: You have to reset the size of currentSetSizeForPopover in viewDidAppear. But you have to be careful, when you will set the same size as was already set in field currentSetSizeForPopover then the popover will not change the size. For this to happen, you can firstly set the fake size (which will be different than one which was set before) followed by setting the proper size. This solution will work even if your controller is nested inside the navigation controller and popover will change its size accordingly when you will navigate back between the controllers.

You could easily create category on UIViewController with the following helper method that would do the trick with setting the size:


- (void) forcePopoverSize {
    CGSize currentSetSizeForPopover = self.contentSizeForViewInPopover;
    CGSize fakeMomentarySize = CGSizeMake(currentSetSizeForPopover.width - 1.0f, currentSetSizeForPopover.height - 1.0f);
    self.contentSizeForViewInPopover = fakeMomentarySize;
    self.contentSizeForViewInPopover = currentSetSizeForPopover;
}

Then just invoke it in -viewDidAppear of desired controller.