9

我正试图在一个特定的Container View上切换两个孩子View Controllers。我有一个Navigation Controller与一个菜单(Table View做出菜单的不同选项)。如何在Swift上切换两个子视图控制器?

每次我点击菜单的一个选项,我想改变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) 
} 

此代码运行得很好。问题是新的子View Controller显示在导航栏和Table View(菜单)上方。所以它占据了整个屏幕,而不是放在容器视图中。

我应该添加更多的东西给我的代码还是我用错误的方式使用我的代码?我已经搜索了很多关于它的内容,但大多数解决方案都是在objective-c中,或者不适合我。

编辑:搜索了很多小时后,我怀疑这是同根View Controllermaster视图控制器连接的嵌入式SEGUE相关的东西,但我不能够嵌入新的孩子到Container View。我试图代码是:

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

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

但他们没有嵌入SEGUE。只需在全屏幕上显示newChild即可。

在此先感谢!

+0

你有没有加入childVC以当前VC的看法? – Horst

+0

我希望你阅读这个:https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html - 它拥有你需要的所有东西和清晰的代码示例。 – Losiowaty

+0

@Losiowaty我已经看过这个页面,但我已经关闭它,因为它看起来并不是我正在使用的Swift。我是全新的,所以也许我错了,我可以同时使用两者。如果我错了,请纠正我。 –

回答

3

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

currentController.containerView.addSubview(newChild.view) 

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

非常感谢!你的第一句话是线索!不过,我觉得我被困在了墙上,因为我认为这是与赛格有关的东西,这是关于容器的东西。 –

0

你叫currentController .addChildViewController(newChild)权利之前,试试这个:

newChild.modalPresentationStyle = .CurrentContext 

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

+0

谢谢您的回复,但它也显示在全屏幕上。我想这可能是关于嵌入赛格的东西,但我无法得到它。看看我的编辑,看看我试过了什么。 –

0

你不想使用presentViewController,我不认为使用segue是正确的,那些会导致全屏幕封面。

我认为你原本是在正确的轨道上,你是否需要views或viewcontrollers?对于UIViews,你可以很容易地用你想要的值填充你的视图,然后用你的新视图替换你的容器视图,你也可以为Xib或故事板场景加载UIView。对于UIViewControllers,我想如果你在他们查看生命周期方法时有特定的逻辑,你需要将它们添加为子视图控制器,并再次用viewController.view替换容器视图。

最后你只需触发你的didselectrowatindexpath方法中的逻辑。试试一个基本的例子,用橙色背景色创建一个UIView,然后做self.containerView = orangeView

让我知道如果这不明确,我很乐意提供代码示例,如果有必要。

0
//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) 
0

容器的制作出口和增加你的孩子控制器这样的:self.addSubview(self.currentViewController!.view, toView: self.containerView)

它可以帮助你。

0

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

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 
相关问题