【问题标题】:Presenting a ViewController breaks the previous ViewController展示一个 ViewController 会破坏之前的 ViewController
【发布时间】:2014-03-03 11:11:06
【问题描述】:

我的问题是当我呈现 UIViewController 时,呈现的视图会变黑。

我有一个名为mainViewControllerUIViewController,它是我的窗口的根视图。 在里面我有一个MMDrawerController(刚刚添加为 mainViewController 视图的子视图)。

MMDrawerController 包含我的其余观点。

但是当我从我的mainViewController 中展示一个新的UIViewController 时,新的 VC 显示良好,但是当它关​​闭时,它只留下黑屏。 注意添加时出现黑屏(我可以直接看到)。解雇时不会。

出于测试目的,我做了这段代码:

UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor redColor];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:vc animated:NO completion:^{
    [vc dismissViewControllerAnimated:YES completion:nil];
}];

这与正常使用的黑色结果相同。 (通常是QLViewController...)

我的 mainViewController 是这样设置的:

_mainViewController = [MyMainViewController new];
_window.rootViewController = _mainViewController;
[self.window addSubview:_mainViewController.view];

Souce code of MMDrawerController 在我的项目中是最新的

【问题讨论】:

  • 您要为哪个版本的 iOS 构建?您是否考虑过使用故事板而不是在代码中创建分配您的 rootViewController?
  • @AshleyMills 我为 iOS 6 和 7 构建,我没有尝试在 iOS6 上解决这个问题,它发生在 iOS7 上(我最关心的......)。我从不在所有应用程序中使用 IB(也不会使用,它不应该与解决方案有任何关系)。
  • 我用你写的代码在一个新项目中进行了测试,但是 mainViewController 仍然是背景色
  • @simalone 问题不在于看起来不错的 mainViewController。它与 (MMDrawerController) 中的其他 UIViewController 一起使用。当我展示我的新UIViewController 时,我的前一个(已经在mainViewController 中)坏了,就像它的约束完全消失了一样。而且我不再在屏幕上看到它(但我猜它仍然是一个子视图)。
  • @AncAinu 所以问题可能在 MMDrawerController 实现代码中,你能列出主要代码吗?

标签: ios uiviewcontroller presentviewcontroller


【解决方案1】:

我已经在 MMDrawerController 示例项目中测试了代码,但我无法重现问题,以下是我尝试过的:

MMAppDelegate.m

-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    //...

    UIViewController *testVc = [[UIViewController alloc] init];
    testVc.view.backgroundColor = [UIColor greenColor];
    [testVc.view addSubview:self.drawerController.view];

    [self.window setRootViewController:testVc];
    [self.window addSubview:testVc.view];

    return YES;
}

MMExampleSideDrawerViewController.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case MMDrawerSectionViewSelection:{
            UIViewController *vc = [UIViewController new];
            vc.view.backgroundColor  = [UIColor redColor];
            UIViewController *mainVC =  [[UIApplication sharedApplication] keyWindow].rootViewController;
            [mainVC presentViewController:vc animated:YES completion:^{
                [vc dismissViewControllerAnimated:YES completion:nil];
            }];

            return;
    //...
}

MMExampleCenterTableViewController.m:

-(void)doubleTap:(UITapGestureRecognizer*)gesture{
    UIViewController *vc = [UIViewController new];
    vc.view.backgroundColor  = [UIColor redColor];
    UIViewController *mainVC =  [[UIApplication sharedApplication] keyWindow].rootViewController;
    [mainVC presentViewController:vc animated:YES completion:^{
        [vc dismissViewControllerAnimated:YES completion:nil];
    }];

    return;

    [self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
}

【讨论】:

  • 你把我引向了正确的方向,伙计。我发现了问题。
【解决方案2】:

最后,问题在于我在 mainViewController 上应用了插入约束。

在幕后,我认为 presentViewController:animated:completion: 方法使用框架而不是 AutoLayout,这会破坏 mainViewControllerUIViewControllerUIView

感谢@simalone,我用同样的方法找到了问题的根源(使用MMDrawerController示例项目)。

我想测试你自己的问题,I uploaded the example project with the issue 这样你就可以理解了。 该行有一个断点,这是错误的起源。 然后只需双击视图。

再次感谢@simalone。干杯!

【讨论】:

    猜你喜欢
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多