微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 模态视图控制器和definitionsPresentationContext的问题

我使用iOS 5中的 new UIViewController container view controller methods创建了一个自定义容器视图控制器.

麻烦的是,即使我的容器控制器的子UIViewController有definesPresentationContext = YES,当它是creates and presents another modal view controller时,UIKit将容器(而不是子容器)设置为呈现控制器.

例如,在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
}

我哪里错了?

我如何确保在模态视图被解除时(而不是容器视图)显示的子视图?

解决方法

在呈现视图控制器之前添加此行:
vc.modalPresentationStyle = UIModalPresentationCurrentContext

如果你已经在视图控制器链中完成了所有正确的父子事物,这将导致呈现的视图替换MyChildViewController的视图,然后当呈现的视图被解除时,MyChildViewController的视图将返回.

哦,我忘了提,即使这样,这只能在iPad上使用.呈现的视图控制器视图始终占据iPhone上的整个屏幕 – 它始终从根视图呈现.

编辑:从iOS 8开始,此功能也可在iPhone上使用. (其次是弹出窗口和拆分视图 – 基本上,大多数“仅在iPad上”的形式的陈述在iOS 8中变得错误,在我看来这是一个很棒的消息.)

原文地址:https://www.jb51.cc/c/117129.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐