【发布时间】:2017-01-12 16:29:24
【问题描述】:
我正在尝试在特定 Container View 上的两个孩子 View Controllers 之间切换。我有一个带有菜单的Navigation Controller(Table View 用于制作菜单的不同选项)。
每次单击菜单选项时,我都想更改 Container View 的子级,但我将子级置于 Navigation bar 和 Table 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 Controller 与 master 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