【问题标题】:How can I switch between two child View Controllers on Swift?如何在 Swift 上的两个子视图控制器之间切换?
【发布时间】:2017-01-12 16:29:24
【问题描述】:

我正在尝试在特定 Container View 上的两个孩子 View Controllers 之间切换。我有一个带有菜单的Navigation ControllerTable View 用于制作菜单的不同选项)。

每次单击菜单选项时,我都想更改 Container View 的子级,但我将子级置于 Navigation barTable View 之上(它们未显示,但位于新子级之下视图控制器)。

我的 Main.storyboard 的方案是这样的:

Navigation Controller --> View Controller (With 2 Container View, 1 for Table View
                                           and the other to set master View Controller)
                                         |
                                         |
                               ------------------------
                               |                      |
                          View Controller        Table View
                            (id: master)

                   View Controller (id: Home)   View Controller (id: screen2)

我在tableView 函数(我检测何时单击菜单选项)上有以下代码来更改容器视图的子视图控制器:

let currentController = self.navigationController?.visibleViewController //container

var oldChild = (currentController?.childViewControllers.last!)! as UIViewController //master
let newChild = (storyboard?.instantiateViewControllerWithIdentifier("Home"))! as UIViewController //Home

if (oldChild != newChild){
    if currentController.childViewControllers.last != nil{
        oldChild.willMoveToParentViewController(nil)
        currentController.navigationController?.navigationBar.willRemoveSubview(oldChild.view)
        //oldChild.view.removeFromSuperview()
        oldChild.removeFromParentViewController()
    }

    currentController.addChildViewController(newChild)
    currentController.view.addSubview(newChild.view)
    newChild.didMoveToParentViewController(currentController) 
}

这段代码运行良好。问题是新的子视图控制器显示在导航栏和表格视图(菜单)上方。所以它占据了全屏而不是适合容器视图。

我应该在我的代码中添加更多内容还是我以错误的方式使用我的代码?我对此进行了很多搜索,但大多数解决方案都在 Objective-c 中,或者对我不起作用。

编辑:在搜索了很多小时后,我怀疑它与将根 View Controllermaster View Controller 连接的嵌入 segue 有关,但我无法嵌入Container View 的新孩子。我正在尝试的代码是:

currentController.performSegueWithIdentifier("EmbedSegue", sender: newChild)

currentController.presentViewController(newChild, animated: true, completion: nil)

但它们都没有嵌入segue。只需全屏显示newChild

提前致谢!

【问题讨论】:

  • 您是否将 childVC 视图添加到当前 VC 的视图中?
  • 我希望你读到这个:developer.apple.com/library/ios/featuredarticles/… - 它有你需要的一切,还有清晰的代码示例。
  • @Losiowaty 我以前看过这个页面,但我已经关闭了它,因为它看起来不像我正在使用的 Swift。我是全新的,所以也许我错了,我可以同时使用两者。如果我错了,请纠正我。
  • 啊,是的,不幸的是代码示例是用 Objective-C 编写的,但不难理解它们,因为它们也被广泛描述,你会注意到几乎所有方法/属性名称都是Obj-C 和 Swift 相同。您可以在一个项目中使用两种语言,但不能在一个文件中使用,尽管您不需要这样做来实现您想要的。
  • @Error404 当然。您的“答案”实际上只是对问题的更新。保留足够多的原始问题以提供任何现有答案的上下文有时会很有帮助,但由于除了您的答案之外没有其他答案,您最好的办法是简单地修改问题以反映您当前的理解。您的问题越容易阅读和理解,就越有可能有人能够帮助您。

标签: ios swift uinavigationcontroller uistoryboardsegue uicontainerview


【解决方案1】:

你应该有 ContainerView 的 IBOutlet 和 addSubview 到这个 IBOutlet

currentController.containerView.addSubview(newChild.view)

currentController.addSubview(childView.view, toView: self.containerView)

【讨论】:

  • 非常感谢!你的第一句话就是线索!我觉得我被困在了墙上,因为我认为这与 segue 有关,但与容器有关。
【解决方案2】:

在你调用 currentController.addChildViewController(newChild) 之前,试试这个:

newChild.modalPresentationStyle = .CurrentContext

这应该在容器视图的范围内而不是全屏显示 newChild 视图控制器。

【讨论】:

  • 感谢您的回复,但它也全屏显示。我想这可能是关于嵌入 segue 的东西,但我还不能得到它。查看我的编辑,看看我尝试了什么。
【解决方案3】:

你不想使用 presentViewController 我也不认为使用 segue 是正确的,那些会导致全屏覆盖。

我认为您最初是在正确的轨道上,您需要视图或视图控制器吗?

对于 UIView,您可以轻松地使用所需的值填充您的视图,然后用新视图替换您的容器视图,您还可以为 xib 或情节提要场景加载 UIView。

对于 UIViewControllers,我猜如果你在他们的视图生命周期方法中有特定的逻辑,你需要将它们添加为子视图控制器,并再次用 viewController.view 替换容器视图。

最后,您只需触发 didselectrowatindexpath 方法中的逻辑即可。只需尝试一个基本示例,创建一个橙色背景颜色的 UIView 并执行self.containerView = orangeView

如果不清楚,请告诉我,如有必要,我很乐意提供代码示例。

【讨论】:

    【解决方案4】:
    //set off the viewcontroller UNDERTOPBAR property off. your VC will load in currentControllers view. you need to set the frame of the view that your are going to load.
    
    currentController.addChildViewController(newChild)
    newChild.view.frame = CGRectMake(self.currentController.bounds.origin.x, self.currentController.bounds.origin.y, self.currentController.bounds.size.width, self.currentController.bounds.size.height)
    currentController.view.addSubview(newChild.view)
    newChild.didMoveToParentViewController(currentController)
    

    【讨论】:

      【解决方案5】:

      制作容器的出口并像这样添加您的子控制器:self.addSubview(self.currentViewController!.view, toView: self.containerView)

      也许对你有帮助。

      【讨论】:

        【解决方案6】:

        问题已得到解答,但正如我在您的代码中看到的那样,我建议您将子视图的边缘固定到父视图的边缘。如下:

            child.view.translatesAutoresizingMaskIntoConstraints = false
            child.view.topAnchor.constraint(equalTo: parent.view.topAnchor).isActive = true
            child.view.bottomAnchor.constraint(equalTo: parent.view.bottomAnchor).isActive = true
            child.view.leadingAnchor.constraint(equalTo: parent.view.leadingAnchor).isActive = true
            child.view.trailingAnchor.constraint(equalTo: parent.view.trailingAnchor).isActive = true
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-10
          • 1970-01-01
          • 2011-05-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多