UIViewController是否可以呈现自己?

时间:2015-01-06 17:17:50

标签: ios objective-c uiviewcontroller

我有一个UIViewController(让我们称之为Fred),它可以嵌入许多不同类型的容器(UINavigationController,UITabBarController或模态中)。弗雷德有一个按钮,当点击时,需要让弗雷德全屏。

直观地说,这意味着我想将Fred作为全屏模式呈现,但有两个问题:

  1. Fred在视图层次结构中处于活动状态(无法显示活动的vc)
  2. 弗雷德需要出示自己?
  3. 我尝试通过创建一个Fred可以这样调用的fullscreenContainerViewController来解决这个问题:

    [fullscreenContainer presentViewControllerAsFullscreen:self]
    

    FullscreenContainerViewController的实现:

    @property (nonatomic) UIViewController* proxyViewController
    
    // Create a proxy VC to hold state of the original ViewController so that we can safely
    // Remove it from it's hierarchy and then restore it after fullscreen is dismissed.
     - (void)presentViewControllerAsFullscreen:(UIViewController*)originalController
    {        
        UIViewController* originalParentViewController = originalController.parentViewController;
        UIView* originalSuperview = originalController.view.superview;
    
        self.proxyViewController = [[UIViewController alloc] init];
        self.proxyViewController.view = [[UIView alloc] initWithFrame:originalController.view.frame];
        self.proxyViewController.view.autoresizingMask = originalController.view.autoresizingMask;
    
        // Detach contentController from parent
        [originalController willMoveToParentViewController:nil];
        [originalController.view removeFromSuperview];
        [originalController removeFromParentViewController];
    
        // Put in the proxy view controller in place of the orignalController
        [originalParentViewController addChildViewController:self.proxyViewController];
        [originalSuperview addSubview:self.proxyViewController.view];
        [self.proxyViewController didMoveToParentViewController:originalParentViewController];
        originalController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self.proxyViewController presentViewController:originalController animated:YES completion:nil];
    }
    

    然后解雇:

    - (void)dismissFullscreenWithCompletionHandler:(void (^)(void))completionHandler
    {
        UIViewController* parentToRestoreTo = self.proxyViewController.parentViewController;
        UIView* superviewToRestoreTo = self.proxyViewController.view.superview;
    
        [self.proxyViewController dismissViewControllerAnimated:YES completion:^(void) {
    
            // Detach proxy view/controller
            [self.proxyViewController removeFromParentViewController];
            [self.proxyViewController.view removeFromSuperview];
            self.proxyViewController = nil;
    
            // Restore original parent relationship
            [parentToRestoreTo addChildViewController:self.contentController];
            [superviewToRestoreTo addSubview:self.contentController.view];
            [self.contentController didMoveToParentViewController:parentToRestoreTo];
            if (self.shouldRestoreStatusBar) {
                [[UIApplication sharedApplication] setStatusBarHidden:NO                       
                                                        withAnimation:UIStatusBarAnimationSlide];
            }
        }];
    }
    

    这可行,但似乎反模式可能吗?也许它会以某种微妙的方式打破?对我正在努力实现的目标有更好的方法吗?

0 个答案:

没有答案