Abbacore
Abbacore

Reputation: 385

UIPopoverController w/ UINavigationController Subview contentSizeForViewInPopover doesnt work on Parent

I have a UIPopoverController with a subclass UINavigationController. Both the parent and child views are UITableviews.

When i call parent view originally with contentSizeForViewInPopover = (320,480) it works great.

When i click into the child view i resize the popover to contentSizeForViewInPopover = (320,780)

When return back to the parent view i cannot get the popover to resize back to contentSizeForViewInPopover = (320,480). the popover stays at the (320,780) size.

Been trying everything but just missing something. Anyone know how resize the view with UIPopoverControllers in the above scenario?

Thanks in Advance!!

Upvotes: 16

Views: 16371

Answers (6)

Denton
Denton

Reputation: 347

After trying many things, the answer by @chuck-k helped me decently resolve for my UINavigationController popover woes in iOS7.

Here's what I did:

  • for each UIViewController within the UINavigationController I calculate the content size I want displayed in method - (CGSize)contentSizeForViewInPopover plus navigationController.navigationBar.frame.size.height (which is always 44 I think). I don't use any other popover functionality in these UIViewControllers.

  • I declared my UIViewController that creates the UINavigationController as a UINavigationControllerDelegate

  • Then in the delegate....

.....

- (void)navigationController:(UINavigationController *)navigationController willShowViewController: (UIViewController *)viewController animated:(BOOL)animated {

    BOOL popoverAnimation = NO;

    if ( self.myPopoverController.popoverContentSize.height < viewController.contentSizeForViewInPopover.height ) popoverAnimation = YES;

    [self.myPopoverController setPopoverContentSize:viewController.contentSizeForViewInPopover animated:popoverAnimation];
}

The height check compares the current view controller's popover content size to the "incoming" view controller popover content size. I use animation = NO when going from a larger --> smaller popover content size, because otherwise I get some jerky repositioning animation in iOS7. But peculiarly if animation = NO when going from a smaller --> larger popover content size, the popover size would increase to the size I was expecting but would not display content larger than the previously smaller content size... setting animation = YES resolved this issue for me. (I only check height because in my case the width is fixed.)

By using this technique almost everything finally works to my satisfaction and I hope this might help someone else.

Upvotes: 0

Chuck H
Chuck H

Reputation: 8266

I had the same problem, but none of the above solutions worked for me. However, in trying to combine their approaches, I was inspired to try a slightly different way to attack the problem. This works for me:

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    viewController.contentSizeForViewInPopover = navigationController.topViewController.view.frame.size;
}

I have three different popovers that each use navigation view controllers. This solution has the nice side effect of working for all of them because it doesn't make a specific reference to any of the popover controllers, but ends up using the popoverContentSize from the popover controller currently being used.

Upvotes: 13

Schoob
Schoob

Reputation: 1718

I think I might have figured this out as I struggled with this issue for a while now. Might be a bit optimistic so please feel free to comment if this solution isn't working for you.

In each viewController displayed with the navigation hierarchy, set the contentSizeForViewInPopover property in the viewDidAppear: method to its appropriate size.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self setContentSizeForViewInPopover:CGSizeMake(320, 320)];
}

Another thing I picked up is that when tapping back while editing a textField, the size stays small instead of the larger previous view. Call the resignFirstResponder method on your textField in the controller's viewWillDisappear.

I'm curious whether this solution works across sdks.

Upvotes: 1

Vadim Fainshtein
Vadim Fainshtein

Reputation: 143

In my project I had to dynamically change the size of the popover.

I made the original controller delegate of my popover content controller, and implemented it's delegate method that is called each time the size is changed:

The code is bellow, hope it helps somebody:

-(void) popover:(UIViewController*)controller didChangeSize:(CGSize)size{

    if ([controller class] == [AZViewController class]){
        if (!_popoverController){
            _popoverController = [[UIPopoverController alloc] initWithContentViewController:controller]; 
        }

        _popoverController.popoverContentSize = size;
    }
}

Upvotes: 2

cristoper
cristoper

Reputation: 470

The contentSizeForViewInPopover property of the view controller only sets the default (initial) size of its containing UIPopoverController. To resize the UIPopoverController at an arbitrary time, you must set its popoverContentSize property. Note that popoverContentSize is a property of the UIPopoverController and not of the view controller (so you'll probably need a reference to the popover controller around).

To reset the popover's size every time a view controller becomes the top view controller of a UINavigationController, you can use the UINavigationControllerDelegate protocol methods:

navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == viewControllerToResize) {
        referenceToUIPopoverController.popoverContentSize = CGSizeMake(320,480);
    }
}

Upvotes: 24

Enrique R.
Enrique R.

Reputation: 810

I think I had the same problem and I solved it by setting the size for the view in the popover every time the view was about to appear. Like this:


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

    self.contentSizeForViewInPopover = CGSizeMake(320, 444); //Set your own size
}

I hope this helps you.

Upvotes: 1

Related Questions