2013-02-17 88 views
12

当我提出我UIViewController与父UINavigationController一套modalPresentationStyleUIModalPresentationCurrentContext,则UIViewController不滑,没有使用过渡。没有动画时modalPresentationStyle设置为UIModalPresentationCurrentContext

这里是我的代码:

UIViewController *viewController = [[UIViewController alloc] init]; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
navController.navigationBarHidden = YES; 

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext; 

[self presentViewController:navController animated:YES completion:nil]; 

当我不设置modalPresenttionStyle,一切工作正常。但我需要这种风格,因为我想把UIViewController作为叠加。

BTW:当ViewController被解雇时,动画工作正常。

+0

你有没有找到一个解决这个? – Alf 2014-02-19 11:00:06

+0

我遇到同样的问题。你有没有找到解决方案? – 2014-05-11 14:48:23

回答

1

如果你想UIViewController被呈现为覆盖是不正确的做法,因为当你这样做: [self presentViewController:navController animated:YES completion:nil]; 你正在做的一个模式呈现,你不会有比你目前的一个父视图控制器。相反,你将有UIWindow,所以它可能会是黑色的,这不是你想要的。

所以为了做你想要什么,你需要出示你的控制器作为childViewController并添加其以你这样的家长控制视图:

UIViewController *viewController = [[UIViewController alloc] init]; 

[self addChildViewController:viewController]; 
[self viewWillDisappear:animated]; 
[self.view addSubview:viewController.view]; 
[self.view bringSubviewToFront:viewController.view]; 
[viewController didMoveToParentViewController:parentController]; 
[self viewDidDisappear:animated]; 

,并删除了UIViewController

[controller.view removeFromSuperview]; 
[controller willMoveToParentViewController:nil]; 
[controller.parentViewController viewDidAppear:animated]; 
[controller removeFromParentViewController]; 
+0

这是错的!如果使用UIModalPresentationCurrentContext,则底层viewController仍然可见!尝试通过呈现一个稍微透明的viewController! – Alexander 2014-07-30 09:04:16

+0

@Alexander我尝试了你说的话,但没有奏效。你能提供一些代码吗?我说的是透明度没有出现在我身上 – 2014-07-30 19:32:05

2

按照UIViewController.h头定义: -

/* 定义过渡的风格,将以模态方式呈现时用于此视图控制器。在视图控制器上设置 这个属性,而不是主持人。默认为 UIModalTransitionStyleCoverVertical。如果你想添加一个覆盖你需要做的是确保你使用的是新的ViewController过渡的第一件事

UIViewController *viewController = [[UIViewController alloc] init]; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
navController.navigationBarHidden = YES; 

//Here is the change 
navController.modalPresentationStyle = UIModalPresentationCurrentContext; 

[self presentViewController:navController animated:YES completion:nil]; 
1

- : */

所以,你应该在presentingViewController这样应用此API的iOS 7.这里有一个快速教程Objc.io View Controller Transitions

一旦你完成你应该有一个动画师和viewcontroller符合UIViewControllerTransitioningDelegate协议。

然后,当您想要呈现控制器时,需要将模态演示样式设置为UIModalPresentationStyleCustom而不是CurrentContext。当然,您的动画师需要配置所呈现的控制器的框架,以便您仍然可以看到下面的内容。

这里还有一个教程,可以帮助 - Custom presentations

最后但并非最不重要的,你将不得不处理演示的情景在任何方向,因为容器旋转时,如果不这样做,你会看到奇怪的行为的过渡仍然是肖像。看到这里我的答案 - transitions in any orientation

1
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"]; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
navController.navigationBarHidden = YES; 

navController.modalPresentationStyle = UIModalPresentationCurrentContext; 

[self presentViewController:navController animated:YES completion:nil]; 

发起的viewController用故事板标识符,它可以帮助

1

让我知道如果这能帮助,当前视图控制器将依次被解雇的动画开始播放。

UIViewController *viewController = [[UIViewController alloc] init]; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
navController.navigationBarHidden = YES; 

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext; 

[self dismissViewControllerAnimated:YES completion:^{ 
    [self presentViewController:navController animated:YES completion:nil]; 
}];