Skip to content

Instantly share code, notes, and snippets.

@stevenchan
Last active August 29, 2015 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenchan/2e6d61eb295bd209896b to your computer and use it in GitHub Desktop.
Save stevenchan/2e6d61eb295bd209896b to your computer and use it in GitHub Desktop.
Detecting if a view controller is presented in a popover with UITraitCollection
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popover
{
if (!self.adaptivePresentationStyleAsked) {
popover.overrideTraitCollection = [popover.presentingViewController.traitCollection copy];
}
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)popover
traitCollection:(UITraitCollection *)traitCollection
{
self.adaptivePresentationStyleAsked = YES;
popover.overrideTraitCollection = [traitCollection copy];
if (traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact ||
traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
return UIModalPresentationOverFullScreen;
} else {
return UIModalPresentationPopover;
}
}
- (void)presentationController:(UIPresentationController *)popover
willPresentWithAdaptiveStyle:(UIModalPresentationStyle)style
transitionCoordinator:(id<UIViewControllerTransitionCoordinator>)transitionCoordinator
{
[popover.presentedViewController willTransitionToTraitCollection:popover.overrideTraitCollection
withTransitionCoordinator:transitionCoordinator];
}
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
if (newCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact ||
newCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
// full screen
} else {
// popover
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment