Last active
August 29, 2015 14:25
-
-
Save kharrison/13080c068cfb26f7d362 to your computer and use it in GitHub Desktop.
Popover Presentation Delegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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