Search code examples
iphoneiosnavigationcontrolleruipopovercontentsize

Change contentSizeForViewInPopover on navigationController push on iOS


I have a UIPopoverController with navigationController and bunch of subviews. The size of the popover is set just before it's shown like this:

[self.myPopover setPopoverContentSize:CGSizeMake(320, 500)];

That works fine. The popover is shown with adjusted size. When another view is pushed on navigation stack the size of a popover is set again - need different height - in viewWillAppear method:

self.contentSizeForViewInPopover = CGSizeMake(320, 700);

This also works fine. When I go back to a previous view the size does not change.

I added the same call in viewWillAppear in first view but the view does not resize.

How can I manage resizing of popover when navigating between views?


Solution

  • I use this hack:

    - (CGSize)contentSizeForViewInPopover
    {
        return CGSizeMake(320, 200);
    }
    
    - (void) forcePopoverSize 
    {
        CGSize currentSetSizeForPopover = self.contentSizeForViewInPopover;
        CGSize fakeMomentarySize = CGSizeMake(currentSetSizeForPopover.width - 1.0f, 
                                              currentSetSizeForPopover.height - 1.0f);
        self.contentSizeForViewInPopover = fakeMomentarySize;
    }
    
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [self forcePopoverSize];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        CGSize currentSetSizeForPopover = self.contentSizeForViewInPopover;
        self.contentSizeForViewInPopover = currentSetSizeForPopover;
    }