Skip to content

Instantly share code, notes, and snippets.

@kharrison
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kharrison/13080c068cfb26f7d362 to your computer and use it in GitHub Desktop.
Save kharrison/13080c068cfb26f7d362 to your computer and use it in GitHub Desktop.
Popover Presentation Delegate
// Set the popover presentation controller when prepare for segue
// The presentation style is set in the storyboard
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"SeguePopover"])
{
self.featuredPPC = segue.destinationViewController.popoverPresentationController;
self.featuredPPC.delegate = self;
}
}
// When adapting to a modal presentation embed the presented VC in a navigation controller if it
// does not already have one and add a button to dismiss
- (UIViewController *)presentationController:(nonnull UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style {
UINavigationController *navController = nil;
if (style != UIModalPresentationNone) {
if ([controller.presentedViewController isKindOfClass:[UINavigationController class]]) {
navController = (UINavigationController *)controller.presentedViewController;
} else {
navController = [[UINavigationController alloc] initWithRootViewController:controller.presentedViewController];
}
UIViewController *rootViewController = navController.viewControllers[0];
rootViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(didDismissPresentedView)];
}
return navController;
}
// Remove the dismiss button if we are no longer modal
- (void)presentationController:(UIPresentationController *)presentationController willPresentWithAdaptiveStyle:(UIModalPresentationStyle)style transitionCoordinator:(nullable id <UIViewControllerTransitionCoordinator>)transitionCoordinator {
if (style == UIModalPresentationNone) {
if ([presentationController.presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController *)presentationController.presentedViewController;
UIViewController *rootViewController = navController.viewControllers[0];
rootViewController.navigationItem.leftBarButtonItem = nil;
}
}
}
- (void)didDismissPresentedView
{
[self.presentedViewController dismissViewControllerAnimated:YES completion:NULL];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment