Search code examples
iosviewcontrollermfmailcomposeviewcontrollerpresentmodalviewcontrolleruiwindow

ModalViewController is hidden under a view


I am working on a photo previewing component. Now I met a problem for showing a modalViewController: When I want to display a preview view, what I am doing is like this:

UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
EKImageViewer *viewer = [[EKImageViewer alloc] initWithFrame:frame];
[window addSubview:viewer];
[window addSubview:viewer.preview];
[window addSubview:viewer.shareBtn];//A UIButton control

When closing the preview view, What I am doing is like this:

[viewer.shareBtn removeFromSuperview];
[viewer.preview removeFromSuperview];
[viewer removeFromSuperview];

When Tapping the shareBtn(UIButton) in preview component, I want to display a modal view(MFMailComposeViewController):

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:controller animated:YES];

There something weird happen: the modalViewController is shown under the preview component. Only when I close the preview component, could I see the the modalViewController. Is There anything I was wrong?


Solution

  • Dont add viewer on top of window. Instead add on your rootViewController.view. That should resolve this. If possible try not to add anything apart from rootViewController on window. Whatever you want to add after that can be added on rootViewController.