【问题标题】:UIAlertController not anchored on childViewController but rather displaying behind it on parentViewControllerUIAlertController 没有锚定在 childViewController 上,而是显示在 parentViewController 的后面
【发布时间】:2019-08-09 09:22:39
【问题描述】:

我创建了一个左侧菜单抽屉并将其添加为子视图控制器。在左侧菜单抽屉中,我有一个应该显示 UIAlertController 的注销按钮。我遇到的挑战是 UIAlertController 显示在父视图的子视图(左抽屉)后面。

//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];
}

我想要在子视图控制器之上显示 UIAlertController。

【问题讨论】:

    标签: ios objective-c childviewcontroller parentviewcontroller childviews


    【解决方案1】:

    问题是您将 menuController.view 添加到堆栈中所有其他视图控制器(包括警报控制器)之上的主窗口。如果您希望菜单位于所有内容之上,但仍位于警报控制器下方,请将其添加到视图树中除窗口之外的其他内容(请参阅当前 VC 或堆栈上更高的内容),这样它就不会出现在警报。

    【讨论】:

    • 这项工作我基本上将 menuController.view 添加到最顶层的视图控制器视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    相关资源
    最近更新 更多