How can I make UIPopoverPresentationController resize on navigation pop?
Asked Answered
B

2

9

I'm using UIPopoverPresentationController for popovers in an iOS app. When a navigation controller in a popover pushes a new view controller, the popover resizes to that view controller's preferredContentSize. But when the navigation controller pops a view controller off the stack, the popover does not resize to the previous size. How can I make it do that?

Possible duplicate of this question, but for the modern UIPopoverPresentationController.

Update: See here for example code illustrating the problem. Clone it and run it in an iPad simulator. Tap the Popover button and you get a popover with a nav controller. Tap the Push bar button item and you get a new taller VC on the stack (the size is in the nav bar). Pop and it doesn't resize back down to what it was.

Bibliophage answered 22/9, 2015 at 14:29 Comment(0)
C
20

Add this line:

self.navigationController.preferredContentSize = self.preferredContentSize;

to your viewWillAppear: method, so that it will now look like this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.navigationController)
    {
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"Push" style:UIBarButtonItemStylePlain target:self action:@selector(onPush)];
        self.navigationItem.rightBarButtonItem = item;
        self.navigationItem.title = NSStringFromCGSize(self.preferredContentSize);
        self.view.backgroundColor = [UIColor lightGrayColor];

        self.navigationController.preferredContentSize = self.preferredContentSize;
    }
}

It will make sure that the navigation controller's preferred size is updated each time the displayed view controller changes. I tested it on your sample code and it resolved the issue.

Cerebrate answered 2/10, 2015 at 10:38 Comment(2)
Nice. I actually have a subclass of UINavigationController that I use to solve this issue, so I overrode all the variants of push and pop and applied this principle there instead of in all the VCs I can have in nav controllers in popovers.Bibliophage
You are a master.Compton
P
2

Just add in your ViewController next code

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    self.navigationController.preferredContentSize = CGSizeMake(200, self.parentViewController.childViewControllers.lastObject.preferredContentSize.height-100);
}

Here is the video. Hope this is behaviour which you expecting to get.

Pavis answered 29/9, 2015 at 17:49 Comment(4)
This looks promising. But can you make it generic, so you don't assume the +/- 100 point size change?Bibliophage
@Mr.Jefferson What do you mean promising? :) You wrote Pop and it doesn't resize back down to what it was. And now you can resize it back to original size. And I've thought that this was just an example from you to illustrate the problem. And make it generic is not clear task :) Can you provide some formula or description how pop-over should react? Because problem was not in generic... :)Pavis
Your answer assumes that the VC you're popping to is going to be 200 points wide and 100 points shorter than the one you're popping from. Can you rewrite your answer to not assume that?Bibliophage
I've used your example from github. If you'll add to your original project provided code above size of popover will change if you'll pop back. P.S. You programmatically added +100 to height for preferredContentSize on push. So you just need to change self.navigationController.preferredContentSize instead of self.preferredContentSize. But if you'll describe what you need in more details - I'll try to solve this. Thanks.Pavis

© 2022 - 2024 — McMap. All rights reserved.