I have a popover containing a UINavigationController. I can display the popover fine, and it contains the navController just fine. The navController contains a tableView a
Much like handling it in viewWillAppear
, another way to deal with this is to override contentSizeForViewInPopover
. Very terse:
-(CGSize)contentSizeForViewInPopover
{
return self.view.bounds.size;
}
I had a similar issue.
I had a popover present from a button in a toolbar. The popover was set to a specific size. It was a table view. When the table row was selected, a new view controller with a navigation controller was called.
When the back button was selected, the popover became the default size (320x1100 I believe), instead of the specific size that I desired.
The original code was:
MyTableViewController *myVC = [[MyTableViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myVC];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(400.0, 500.0);
[myVC release];
[navController release];
[popover release];
I added one line to solve the problem. Granted it is kind of a work around because I had to subtract the height of the header. Maybe one of you could enlighten me with a better method. Anyway, it works.
MyTableViewController *myVC = [[MyTableViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myVC];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(400.0, 500.0);
//Subtract the height of the header to match the total popover size (not just the view).
myVC.contentSizeForViewInPopover = CGSizeMake(400.0, 500-44);
[myVC release];
[navController release];
[popover release];
I believe that when a nav controller is involved, and the back button is pressed, it causes the popover to default to its default size. By adding the contentSizeForViewInPopover property for the view controller myVC, it forces the specific size.
Hope this is helpful.
Kurt
Don't work for me, when i use this:
[UIPopoverController
[UINavigationController] = root vc = [UIViewController vc1] => [UIViewController vc2]
]
When appear popover is all good, press button on vc1 and push vc2 to navigation controller
Next return to vc1 (root) pressing button in vc2 (popToRootViewController method);
We can see that popover change own size, but vc1 size is old... WHAT IS THIS???
Ok, now work... Add popover property in my controller
self.contentSizeForViewInPopover = (CGSize){400, 200};
self.navigationController.contentSizeForViewInPopover = self.contentSizeForViewInPopover;
self.popover.popoverContentSize = self.contentSizeForViewInPopover;
In response to graphical glitches with animations:
The UIPopoverController animations conflict with the UINavigation controllers animations, if you create the popover with a UINavigationController inside it. It results in graphical glitches when animating. To fix the issue, set animated parameter to false when pushing other controllers, or when displaying the toolbar.
Pushing View Controllers:
[self.navigationController pushViewController:detailViewController animated:NO];
Making the Toolbar visible:
[[self navigationController] setToolbarHidden:NO animated:NO];
Setting the animated:NO will make the animations look correct in a UIPopoverController.
Why not just set the contentSizeForViewInPopover
before you push the next controller onto the navigation stack? No need to set sizes in viewWillAppear
and such.
[nextController setContentSizeForViewInPopover:[self contentSizeForViewInPopover]];
[[self navigationController] pushViewController:nextController animated:YES];
Works on IOS 5.1
Running Swift 4 and iOS 11 the only possible solution was for me. To use showViewController:sender: and showDetailViewController:sender:
instead of presentViewController:animated:completion:
.
From the Apple Doc
The showViewController:sender: and showDetailViewController:sender: methods offer the most adaptive and flexible way to display view controllers. These methods let the presenting view controller decide how best to handle the presentation. For example, a container view controller might incorporate the view controller as a child instead of presenting it modally. The default behavior presents the view controller modally.