问题描述
我已使用在iOS 5中。
I've created a custom container view controller using the new UIViewController container view controller methods in iOS 5.
问题是,即使我的容器控制器子UIViewController有,当它,UIKit将容器(而不是子)设置为呈现控制器。
Trouble is, even though my container controller's child UIViewController has definesPresentationContext = YES
, when it creates and presents another modal view controller, UIKit sets the container (rather than the child) as the presenting controller.
例如, MyChildViewController.m:
For example, in MyChildViewController.m:
- (void)showMailComposeView:(id)sender {
__block MFMailComposeViewController *vc =
[[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
vc.subject = @"Subject";
self.definesPresentationContext = YES;
[self presentViewController:vc animated:YES completion:^{
if ([self.modalViewController isEqual:vc])
NSLog(@"This should print...");
if ([vc.presentingViewController isEqual:self.parentViewController])
NSLog(@"... but this shouldn't");
// NOTE: Both log statements printed
}];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{
[self dismissViewControllerAnimated:YES completion:^{}];
// NOTE: self.parentViewController.view now displays instead of self.view
}
我错了什么?
如何确保它是子视图,
推荐答案
在呈现视图控制器之前添加此行:
Add this line before presenting the view controller:
vc.modalPresentationStyle = UIModalPresentationCurrentContext
如果您已经在视图控制器链中完成了所有正确的父子对象,这将导致呈现的视图替换MyChildViewController的视图,然后当提交的视图被关闭时,MyChildViewController的视图将返回。
If you've done all the correct parent-child things all the way up the view controller chain, this will cause the presented view to replace the MyChildViewController's view, and then the MyChildViewController's view will return when the presented view is dismissed.
哦,我忘了提及,即使这样只会在iPad上工作。
Oh, and I forgot to mention, even then this will work only on iPad. A presented view controller's view always occupies the whole screen on iPhone - it is always presented from the root view.
编辑:从iOS 8开始,此功能也可在iPhone上使用。 (因此是popovers和split视图 - 基本上,形式只在iPad上的大多数声明变成了假的iOS 8,在我看来是真棒的消息。)
Starting in iOS 8, this feature is also available on iPhone. (And so are popovers and split views - basically, most statements of the form "only on iPad" became false with iOS 8, which in my view is awesome news.)
这篇关于问题与Modal视图控制器和definitionsPresentationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!