2013-02-17 84 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]; 
}];