xcode 关闭模态视图控制器会导致黑屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26641991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dismissing modal view controller results in a black screen
提问by artooras
This is my view (controller) hierarchy:
这是我的视图(控制器)层次结构:
UITabBarController
(as therootViewController
of the app)UINavigationController
(as theviewController
for one of thetabBar
tabs)UIViewController
(as therootViewController
of theUINavigationController
)UICollectionView
(as a subview)MyViewController.view
(as a section header view of theUICollectionView
)
UITabBarController
(作为rootViewController
应用程序的)UINavigationController
(作为viewController
用于之一tabBar
标签)UIViewController
(作为rootViewController
的UINavigationController
)UICollectionView
(作为子视图)MyViewController.view
(作为 的部分标题视图UICollectionView
)
And so, I need to present a modal view controller from MyViewController
. I have tried doing it with
因此,我需要从MyViewController
. 我试过这样做
[self presentViewController:modalVC animated:YES completion:nil];
and although it worked, Xcode warned me that "Presenting view controllers on detached view controllers is discouraged", and rightly so, because the modalVC only fills the view of the collection view header, which is not full screen which I'm after.
虽然它有效,但 Xcode 警告我“不鼓励在分离的视图控制器上呈现视图控制器”,这是正确的,因为 modalVC 只填充集合视图标题的视图,这不是我所追求的全屏。
All other options that I have tried:
我尝试过的所有其他选项:
UITabBarController *tb = (UITabBarController *)self.view.window.rootViewController;
[tb presentViewController:modalVC animated:YES completion:nil];
or...
UINavigationController *nc = (UINavigationController *)tb.selectedViewController;
[tb presentViewController:modalVC animated:YES completion:nil];
or...
UICustomViewController *cv = (UICustomViewController *)nc.topViewController;
[vc presentViewController:modalVC animated:YES completion:nil];
present the modalVC full screen as desired, however, when I dismiss the modalVC by calling
根据需要呈现 modalVC 全屏,但是,当我通过调用关闭 modalVC
[self dismissViewControllerAnimated:YES completion:nil];
from within modalVC itself, modalVC indeed dismisses itself, but I am left with a black screen. A little debugging revealed that after dismissing modalVC, self.view.window.rootViewController
becomes nil
.
从 modalVC 本身来看,modalVC 确实自己解散了,但我留下了一个黑屏。一个小小的调试发现,在关闭modalVC之后,self.view.window.rootViewController
变成了nil
.
Any idea why this is happening and how to resolve this?
知道为什么会发生这种情况以及如何解决这个问题吗?
EDIT
编辑
This is an iPhone app. The black screen happens on both iOS7 and iOS8. Also, below is the method where I initiate MyViewController
这是一个 iPhone 应用程序。黑屏发生在 iOS7 和 iOS8 上。另外,下面是我发起的方法MyViewController
#pragma mark - UICollectionViewDelegate methods
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
self.myViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];
return self.myViewController.view.frame.size;
}
采纳答案by artooras
I found the solution - this answerreally helped. The trick was in dismissing the view controller. It should be done like this:
我找到了解决方案 -这个答案真的很有帮助。诀窍是关闭视图控制器。应该这样做:
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
and not like this:
而不是这样:
[self dismissViewControllerAnimated:YES completion:nil];
Although the author of the linked answer suggests that a better approach would be to use delegation (presentED VC would define a protocol and presentING VC would subscribe to it and then dismiss the presentED VC once it asks for it), it wasn't feasible in my case.
尽管链接答案的作者建议更好的方法是使用委派(presentED VC 将定义一个协议,presentING VC 将订阅它,然后在提出请求时取消presentED VC),但这在我的情况。
回答by Klaus Thul
Is this an iPad app? If yes, adding
这是 iPad 应用程序吗?如果是,添加
modalVC.modalPresentationStyle = UIModalPresentationFullScreen
before
前
[self presentViewController:modalVC animated:YES completion:nil];
in MyViewController
might do the trick.
在MyViewController
可能做的伎俩。
回答by alla
I find some information in Apple's UIViewController file
我在 Apple 的 UIViewController 文件中找到了一些信息
//The next two methods are replacements for presentModalViewController:animated and //dismissModalViewControllerAnimated: The completion handler, if provided, will be invoked after the presented controllers viewDidAppear: callback is invoked.
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0); // The completion handler, if provided, will be invoked after the dismissed controller's viewDidDisappear: callback is invoked.
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion NS_AVAILABLE_IOS(5_0);
//接下来的两个方法是对presentModalViewController:animated和//dismissModalViewControllerAnimated的替换:如果提供了完成处理程序,将在调用呈现的控制器viewDidAppear:回调之后调用。
- (void)presentViewController:(UIViewController *)viewControllerToPresent animation: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0); // 完成处理程序(如果提供)将在调用已解除控制器的 viewDidDisappear: 回调后调用。
- (void)dismissViewControllerAnimated: (BOOL)flag 完成:(void (^)(void))completion NS_AVAILABLE_IOS(5_0);
As you describled,you directly add view in UIViewController to another view . So viewDidAppear and viewDidDisappear are not invoked.I guess you'd better execute these two methods manually.
正如您所描述的,您直接将 UIViewController 中的视图添加到另一个视图。所以 viewDidAppear 和 viewDidDisappear 没有被调用。我想你最好手动执行这两个方法。