【问题标题】:Is it possible to add a table view controller to a part of a view controller?是否可以将表视图控制器添加到视图控制器的一部分?
【发布时间】:2019-03-20 21:06:39
【问题描述】:

假设我有一个 UITableViewController,它大部分是可重用的,应该在许多 UIViewController 中使用,但它应该只覆盖整个视图的一部分(例如总高度的 90%)。通常我会通过导航来做到这一点,但如果我想保持 UIViewController 的前 10% 可见,并为剩余的 90% 显示 UITableViewController ,这是可能的,如果是的话怎么做?

【问题讨论】:

  • 在这方面,表格视图控制器没有什么特别之处。几乎任何视图控制器都可以包含在另一个视图控制器中。
  • 你可以使用容器视图来做到这一点——将一个容器视图拖到视图控制器中,你可以将容器视图连接到表视图控制器,使其成为父视图控制器的子视图控制器。

标签: ios uiviewcontroller uinavigationcontroller


【解决方案1】:

是的。大视图控制器是容器视图控制器,小视图控制器(在这种情况下是表视图控制器)是子视图控制器。我们可以在容器视图控制器中添加或移除子视图控制器。

将子视图控制器添加到容器中

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

从容器中移除子视图控制器

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

我们也可以同时移除一个旧的子视图控制器并添加一个新的子视图控制器。这是示例代码(带动画)。

- (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];
        }];
}

【讨论】:

    【解决方案2】:

    是的,你可以。只需将 UITableViewController 作为子控制器添加到您的父 UIViewController。

    另外,你可以在这里阅读Apple Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 2016-01-03
      • 2013-06-21
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多