iOS Container View Controller

Keywords: iOS github Attribute

iOS Container View Controller

A controller consists of one or more other controllers, the former being Container View Controller and the latter being Child View Controller. UINavigation Controller and UITabBarController are commonly used container controllers. This paper introduces the method of customizing container controller.

Custom Container Controller

Adding sub-controllers

- (void)displayContentController:(UIViewController *)content {
   [self addChildViewController:content];
   content.view.frame = [self frameForContentController];
   [self.view addSubview:self.currentClientView];
   [content didMoveToParentViewController:self];
}

Note that the addChildViewController: method of the container controller calls the willMoveToParentViewController: method of the subcontroller, so there is no need to write the willMoveToParentViewController: method of the subcontroller.

Remove subcontrollers

- (void)hideContentController:(UIViewController *)content {
   [content willMoveToParentViewController:nil];
   [content.view removeFromSuperview];
   [content removeFromParentViewController];
}

Note that the removeFromParentViewController method of the subcontroller calls didMoveToParentViewController: method instead of writing didMoveToParentViewController: method.

Transition between subcontrollers

- (void)cycleFromViewController:(UIViewController *)oldVC
               toViewController:(UIViewController *)newVC {
   // Prepare the two view controllers for the change.
   [oldVC willMoveToParentViewController:nil];
   [self addChildViewController:newVC];
 
   // Get the start frame of the new view controller and the end frame
   // for the old view controller. Both rectangles are offscreen.
   newVC.view.frame = [self newViewStartFrame];
   CGRect endFrame = [self oldViewEndFrame];
 
   // Queue up the transition animation.
   [self transitionFromViewController:oldVC toViewController:newVC
        duration:0.25 options:0
        animations:^{
            // Animate the views to their final positions.
            newVC.view.frame = oldVC.view.frame;
            oldVC.view.frame = endFrame;
        }
        completion:^(BOOL finished) {
           // Remove the old view controller and send the final
           // notification to the new view controller.
           [oldVC removeFromParentViewController];
           [newVC didMoveToParentViewController:self];
        }];
}

Container Controller Transition FromViewController: to ViewController: duration: options: animations: completion: This method adds the view of the new VC, executes the animations block, and removes the view of the old VC at the end of the animation.

Appearance and disappearance of notification sub-controller

- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}

If this sentence is added, the container controller should notify the sub-controller when the sub-control appears or disappears, and realize it by calling the begin Appearance Transition: animated: method and endAppearance Transition () method of the sub-controller respectively. Do not directly call the viewWillAppear:, viewDidAppear:, viewWillDisappear:, viewDidDisappear: method of the sub-controller.

Delegate sub-controller

Overload the child ViewController ForStatusBarStyle property, return the corresponding sub-controller, and let the sub-controller determine the status bar style. When this property changes, the setNeedsStatusBarAppearance Update () method is called to update the status bar style.

The container controller can use the preferred ContentSize attribute of the sub-controller to determine the size of the sub-controller view.

Third Party Container Controller

ViewDeck

https://github.com/ViewDeck/ViewDeck

The left and right side sliding view realizes the function of side sliding menu.

SWScrollViewController

https://github.com/Silence-GitHub/SWScrollViewController

Scroll view includes the view of sub-controller, which can slide the sub-controller from left to right.

SWSegmentedController

https://github.com/Silence-GitHub/SWSegmentedController

Switch subcontrollers through UISegmented Control.

For reprinting, please indicate the source: http://www.cnblogs.com/silence-cnblogs/p/6370049.html

Posted by mkili on Sat, 23 Mar 2019 04:51:54 -0700