首页 > 代码库 > View-Controller-Containment

View-Controller-Containment

  1. willMove(toParentViewController:)
    • 调用时机
    • 调用addChildViewController(_:)以钱会被自动调用
    • 调用removeFromParentViewController()之前被手动调用。
  2. didMove(toParentViewController:)

    • 调用时机
    • 调用removeFromParentViewController()方法之后被自动调用
    • 调用addChildViewController(_:)方法之后被手动调用。
  3. 把子vc的view加到父vc的view上

    [self addChildViewController:_startMapViewController];          //  1  作为子VC,会自动调用子VC的willMoveToParentViewController方法。
    [topContainer addSubview:_startMapViewController.view];         //  2 子VC的view被作为subview添加到第一个viewContainer中
    [_startMapViewController didMoveToParentViewController:self];   //  3 子VC被通知拥有一个父VC。
    
  4. 把子VC的view移除

    [fromController willMoveToParentViewController:nil];//
    调用转场方法或者[fromController.view removeFromSuperView]
    [fromController removeFromParentViewController];    // 
    

5 transition(from:to:duration:options:animations:completion:)

This method adds the second view controller'??s view to the view hierarchy and then performs the animations defined in your animations block. After the animation completes, it removes the first view controller'??s view from the view hierarchy.

这个函数首先把第二个VC的view加到父vc的字view上,然后执行动画,最后把第一个vc的view从view hierarchy中移除。

{
    toController.view.frame = fromController.view.bounds;                           //  1
    [self addChildViewController:toController];                                     //  
    [fromController willMoveToParentViewController:nil];                            //  

    [self transitionFromViewController:fromController
                      toViewController:toController
                              duration:0.2
                               options:direction | UIViewAnimationOptionCurveEaseIn
                            animations:nil
                            completion:^(BOOL finished) {

                                [toController didMoveToParentViewController:self];  //  2
                                [fromController removeFromParentViewController];    //  3
                            }];
}

这个函数是把fromVC及其View移除,toVC及其View加到界面上去。

  • fromVC移除

    [fromController willMoveToParentViewController:nil]; //函数调用前
    [fromController removeFromParentViewController];    //  动画block中
    [fromController.view removeFromSuperView] //动画block之后
    
  • toVC加到界面上

    [self addChildViewController:toController]; //函数调用前
    [self.view addSubView:toController.view] //动画block之前       
    [toController didMoveToParentViewController:self];  //  block之中
    

View Controller Containment

View-Controller-Containment