5

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?

Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91

2 Answers2

3

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;
}
Sergey Kuryanov
  • 6,114
  • 30
  • 52
Injectios
  • 2,777
  • 1
  • 30
  • 50
  • 1
    http://stackoverflow.com/questions/2752394/popover-with-embedded-navigation-controller-doesnt-respect-size-on-back-nav – Injectios Mar 15 '12 at 10:35
3

This is a tricky one. I tried many things and finally got this one working. It just might work for you, too.

In my contentViewController i keep a reference to UIPopoverController *parent;

This reference is set during initialization of UIPopoverController and it's content. This of course might not fit directly into your view hierarchy. Code in UIViewController that shows UIPopowerController is something like:

if (self.popoverController == nil) {
    _contentController = [[ContentViewController alloc] initWithNibName:@"ContentViewController"
                                                                 bundle:[NSBundle mainBundle]]; 

    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:_contentController]; 
    _contentController.parent = popover;
    [popover setPopoverContentSize: CGSizeMake(520.0,580.0)];

    popover.delegate = self;

    self.popoverController = popover;
}

And when i want to dynamically change content size from within contentViewController i use:

-(void)setNewSize:(CGSize) newSize {

    [_parent setPopoverContentSize:newSize animated:YES];
}
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • 1
    @BorutTomazin: i know - it's actually ugly :) but after 8 hours of work i finally found one thing that worked. I'll probably try to find more elegant solution for this once... probably. I'll try the link you posted above. – Rok Jarc Mar 15 '12 at 12:16
  • 1
    Yeah I know. Sometimes is ugly and working better than nice and not working. :) Let me know if you find better solution. Thanks! – Borut Tomazin Mar 15 '12 at 14:20