Can't resize height contentSizeForViewInPopover of UIViewController in side UINavigationController of UIPopover

StackOverflow https://stackoverflow.com/questions/17943731

Question

I use UINavigationController inside UIPopoverController

-(void)showEditMenuFrom:(UIButton *)button{
    if (self.popover) {
        [self.popover dismissPopoverAnimated:YES];
        self.popover = nil;
    }
    else {
        EditMenuViewController *editMenuViewController = [[EditMenuViewController alloc] initWithNibName:@"EditMenuViewController" bundle:nil];

        UINavigationController *actionsNavigationController = [[UINavigationController alloc] initWithRootViewController:editMenuViewController];
        actionsNavigationController.delegate = self;

        // switch for iPhone and iPad.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
            self.popover.delegate = self;
            //            CGRect presentFrame = CGRectMake(button.frame.origin.x-43, button.frame.origin.y-10, button.frame.size.width, button.frame.size.height);
            [self.popover presentPopoverFromRect:button.frame inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        } else {
            [self presentViewController:actionsNavigationController animated:YES completion:^{
                NSLog(@"Activity complete");
            }];
        }
    }

    }

And I try to resize view inside UIPopover when navigate and these are result:

  1. Root view:

enter image description here

2.Push to other VC

enter image description here

3.Pop back:

enter image description here

You can see the height of root VC can't change back to origin size.

I try to set contentSizeForViewInPopover in viewDidAppear, viewDidLoad and in UINavigationControllerDelegate but no methods work

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSLog(@"Show VIEW %@",viewController);
    if ([viewController isKindOfClass:[EditMenuViewController class]]) {
        viewController.contentSizeForViewInPopover = CGSizeMake(160.0, 160.0);
    } else {
        viewController.contentSizeForViewInPopover = CGSizeMake(320.0, 320.0);
    }
}

How to solve this problem? Thanks!

Was it helpful?

Solution

I end up my question for someone meet this solution:

First, you need to declare a @property inside the VC which pop back on stack

@property (nonatomic, strong) UIPopoverController *popover;

Then assign it to UIPopoverController in main code

self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
self.popover.delegate = self;

editMenuViewController.popover = self.popover;

And Finally, setting the size in viewWillAppear of VC you want to resize

-(void)viewWillAppear:(BOOL)animated
{
    [self.popover setPopoverContentSize:CGSizeMake(160,160)];
    self.contentSizeForViewInPopover = CGSizeMake(160,160);
    [super viewWillAppear:animated];
}

OTHER TIPS

Try with bellow example miht be usefull:-

-(void)showEditMenuFrom:(UIButton *)button{
    if (self.popover) {
        [self.popover dismissPopoverAnimated:YES];
        self.popover = nil;
    }
    else {
        EditMenuViewController *editMenuViewController = [[EditMenuViewController alloc] initWithNibName:@"EditMenuViewController" bundle:nil];

        UINavigationController *actionsNavigationController = [[UINavigationController alloc] initWithRootViewController:editMenuViewController];
        actionsNavigationController.delegate = self;

        // switch for iPhone and iPad.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
            self.popover.delegate = self;

            popover.popoverContentSize =CGSizeMake(250,200); //Addint this line for setContentsize


            [self.popover presentPopoverFromRect:button.frame inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        } else {
            [self presentViewController:actionsNavigationController animated:YES completion:^{
                NSLog(@"Activity complete");
            }];
        }
    }

    }

In EditMenuViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.clearsSelectionOnViewWillAppear = NO; //this work if superclass is UITableviewcontroller
     self.contentSizeForViewInPopover = CGSizeMake(200,100);
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.contentSizeForViewInPopover = CGSizeMake(250,200);
    [super viewWillDisappear:YES];
}

ADD

Check some useful similar to your issue:-

Animate popoverContentsize when pushing navigation view controller in popover on iPad

UIPopoverController automatically resizing to max height on pushViewController

push a new tableViewController in a uipopovercontroller causes the popover to be resized

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top