iosobjective-cchildviewcontrollerparentviewcontrollerchildviews

UIAlertController not anchored on childViewController but rather displaying behind it on parentViewController


I have created a left menu drawer and added it as a child view controller. In the left menu drawer I have a logout button that should display a UIAlertController. The challenge I am experiencing is that the UIAlertController is displaying behind the child view (left drawer) on the parent view.

//ParentVC 
//adding childVC
  DrawerViewController *menuController = [[DrawerViewController alloc] init];
  [self addChildViewController:menuController];
  [self.view addSubview:menuController.view];
  [menuController didMoveToParentViewController:self];
  [[[[UIApplication sharedApplication] delegate] window] addSubview:menuController.view];
  menuController.definesPresentationContext = YES;

//displaying childVC from parentVC
  [UIView animateWithDuration:0.3 animations:^{
    [menuController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view setFrame:CGRectMake(self.view.frame.size.width*0.7, 0, self.view.frame.size.width, self.view.frame.size.height)];

  }];


//ChildVC
//logout action
-(void) logOutButtonListener:(UIButton *) sender{
NSLog(@"logOutButtonListener");

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Logout" message:@"Are you sure you want to logout?" preferredStyle:UIAlertControllerStyleAlert ];

UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"no" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:noButton];

UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    //logout logic
}];
[alert addAction:yesButton];

[self presentViewController:alert animated:YES completion:nil];
}

What I want is to show the UIAlertController on top of the child view controller.


Solution

  • The problem is that you are adding the menuController.view to the main window on top of all of the other viewcontrollers in your stack including the alert controller. If you want the menu to be above everything, but still under the alert controller, add it to something in the view tree other than the window (see the current VC or something higher up the stack) so it doesn't appear on top of the alert.