在容器视图中交换子视图

时间:2013-10-03 15:15:48

标签: ios objective-c animation transitions uicontainerview

ContainerView成为包含两个子内容视图的父容器视图:NavigationViewContentView

Example of View Layout

我希望能够将ContentView的控制器换成另一个视图。例如,使用新闻页面控制器交换主页控制器。目前,我能想到的唯一方法是使用委托告诉ContainerView我要切换视图。这似乎是一种草率的方式,因为ContainerViewController最终会为所有子视图提供一堆特殊的委托。

这还需要与NavigationView进行通信,ContentView包含有关ContentView中当前视图的信息。例如:如果用户在新闻页面上,导航视图中的导航栏将显示当前选择了新闻按钮。

问题A: 有没有办法在ContainerView中交换控制器而没有委托方法调用ContentView本身?我想以编程方式(没有故事板)这样做。

问题B: 如何在没有委托电话的情况下从NavigationView交换{{1}}中的控制器?我想以编程方式(没有故事板)这样做。

3 个答案:

答案 0 :(得分:38)

当您拥有拥有自己的视图控制器的子视图时,您应该遵循自定义容器控制器模式。有关详细信息,请参阅Creating Custom Container View Controllers

假设您已经遵循自定义容器模式,当您想要更改“内容视图”的子视图控制器(及其关联视图)时,您可以通过以下方式进行编程:

UIViewController *newController = ... // instantiate new controller however you want
UIViewController *oldController = ... // grab the existing controller for the current "content view"; perhaps you maintain this in your own ivar; perhaps you just look this up in self.childViewControllers

newController.view.frame = oldController.view.frame;

[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController];         // incidentally, this does the `willMoveToParentViewController` for the new controller for you

[self transitionFromViewController:oldController
                  toViewController:newController
                          duration:0.5
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            // no further animations required
                        }
                        completion:^(BOOL finished) {
                            [oldController removeFromParentViewController]; // incidentally, this does the `didMoveToParentViewController` for the old controller for you
                            [newController didMoveToParentViewController:self];
                        }];

当你这样做时,不需要任何与内容视图控制器的委托协议接口(除了iOS已经提供的Managing Child View Controllers in a Custom Container方法)。


顺便说一下,这假设与内容视图关联的初始子控制器是这样添加的:

UIViewController *childController = ... // instantiate the content view's controller any way you want
[self addChildViewController:childController];
childController.view.frame = ... // set the frame any way you want
[self.view addSubview:childController.view];
[childController didMoveToParentViewController:self];

如果您希望子控制器告诉父级更改与内容视图关联的控制器,您可以:

  1. 为此定义协议:

    @protocol ContainerParent <NSObject>
    
    - (void)changeContentTo:(UIViewController *)controller;
    
    @end
    
  2. 定义父控制器以符合此协议,例如:

    #import <UIKit/UIKit.h>
    #import "ContainerParent.h"
    
    @interface ViewController : UIViewController <ContainerParent>
    
    @end
    
  3. 在父控制器中实现changeContentTo方法(如上所述):

    - (void)changeContentTo:(UIViewController *)controller
    {
        UIViewController *newController = controller;
        UIViewController *oldController = ... // grab reference of current child from `self.childViewControllers or from some property where you stored it
    
        newController.view.frame = oldController.view.frame;
    
        [oldController willMoveToParentViewController:nil];
        [self addChildViewController:newController];
    
        [self transitionFromViewController:oldController
                          toViewController:newController
                                  duration:1.0
                                   options:UIViewAnimationOptionTransitionCrossDissolve
                                animations:^{
                                    // no further animations required
                                }
                                completion:^(BOOL finished) {
                                    [oldController removeFromParentViewController];
                                    [newController didMoveToParentViewController:self];
                                }];
    }
    
  4. 子控制器现在可以使用此协议来引用iOS为您提供的self.parentViewController属性:

    - (IBAction)didTouchUpInsideButton:(id)sender
    {
        id <ContainerParent> parentViewController = (id)self.parentViewController;
        NSAssert([parentViewController respondsToSelector:@selector(changeContentTo:)], @"Parent must conform to ContainerParent protocol");
    
        UIViewController *newChild = ... // instantiate the new child controller any way you want
        [parentViewController changeContentTo:newChild];
    }
    

答案 1 :(得分:2)

对于此类过渡,您还可以使用UIView.animateWith...动画。

例如,假设rootContainerView是容器中的容器和contentViewController当前活动的控制器,那么<​​/ p>

func setContentViewController(contentViewController:UIViewController, animated:Bool = true) {
    if animated == true {
        addChildViewController(contentViewController)
        contentViewController.view.alpha = 0
        contentViewController.view.frame = rootContainerView.bounds
        rootContainerView.addSubview(contentViewController.view)
        self.contentViewController?.willMoveToParentViewController(nil)

        UIView.animateWithDuration(0.3, animations: {
            contentViewController.view.alpha = 1
            }, completion: { (_) in
                contentViewController.didMoveToParentViewController(self)

                self.contentViewController?.view.removeFromSuperview()
                self.contentViewController?.didMoveToParentViewController(nil)
                self.contentViewController?.removeFromParentViewController()
                self.contentViewController = contentViewController
        })
    } else {
        cleanUpChildControllerIfPossible()

        contentViewController.view.frame = rootContainerView.bounds
        addChildViewController(contentViewController)
        rootContainerView.addSubview(contentViewController.view)
        contentViewController.didMoveToParentViewController(self)
        self.contentViewController = contentViewController
    }
}

// MARK: - Private

private func cleanUpChildControllerIfPossible() {
    if let childController = contentViewController {
        childController.willMoveToParentViewController(nil)
        childController.view.removeFromSuperview()
        childController.removeFromParentViewController()
    }
}

这将提供简单的淡入淡出动画,你也可以尝试任何UIViewAnimationOptions,过渡等。

答案 2 :(得分:-1)

你好像很困惑。 contentView(假设UIView)不包含您要换出的控制器。 UIViewController处理其UIView。在我看来,你需要一个父子视图控制器设置。

单个父视图控制器,它将处理子视图控制器,然后您可以在屏幕上显示每个控制器时处理每个视图控制器,并相应地调整您的UIViews和内容。请参阅下面的Apple文档。

Container Programming - Apple Documentation