Change contentSizeForViewInPopover on navigationController push on iOS
-
16-12-2019 - |
Question
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;
}
OTHER TIPS
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];
}