This is my view (controller) hierarchy:
UITabBarController
(as therootViewController
of the app)UINavigationController
(as theviewController
for one of thetabBar
tabs)UIViewController
(as therootViewController
of theUINavigationController
)UICollectionView
(as a subview)MyViewController.view
(as a section header view of theUICollectionView
)
And so, I need to present a modal view controller from MyViewController
. I have tried doing it with
[self presentViewController:modalVC animated:YES completion:nil];
and although it worked, Xcode warned me that "Presenting view controllers on detached view controllers is discouraged", and rightly so, because the modalVC only fills the view of the collection view header, which is not full screen which I'm after.
All other options that I have tried:
UITabBarController *tb = (UITabBarController *)self.view.window.rootViewController;
[tb presentViewController:modalVC animated:YES completion:nil];
or...
UINavigationController *nc = (UINavigationController *)tb.selectedViewController;
[tb presentViewController:modalVC animated:YES completion:nil];
or...
UICustomViewController *cv = (UICustomViewController *)nc.topViewController;
[vc presentViewController:modalVC animated:YES completion:nil];
present the modalVC full screen as desired, however, when I dismiss the modalVC by calling
[self dismissViewControllerAnimated:YES completion:nil];
from within modalVC itself, modalVC indeed dismisses itself, but I am left with a black screen. A little debugging revealed that after dismissing modalVC, self.view.window.rootViewController
becomes nil
.
Any idea why this is happening and how to resolve this?
EDIT
This is an iPhone app. The black screen happens on both iOS7 and iOS8. Also, below is the method where I initiate MyViewController
#pragma mark - UICollectionViewDelegate methods
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
self.myViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];
return self.myViewController.view.frame.size;
}
MyViewController
andMyViewController.view
? – Aaron BragerMyViewController
itself. – artooras