我刚刚做了一个小的转换,所以我的项目加载了一个不同的 Storyboard作为其主要内容(在 info.plist 中进行)。
我有我的新 Storyboard来保存负责登录屏幕等的viewController。只是为了让它更清楚。
点击登录按钮后,我想从另一个 Storyboard启动导航 Controller :
func instantiateViewController(fromStoryboard storyboard: String, withIdentifier identifier: String) -> UIViewController! {
let storyboard = UIStoryboard(name: storyboard, bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier(identifier)
return viewController
}
@IBAction func loginButtonTapped(sender: UIButton) {
let viewController = instantiateViewController(fromStoryboard: "Main", withIdentifier: "MainNavigationController")
presentViewController(viewController, animated: true, completion: nil)
}
一切正常,但有一件事让我发疯。 从 Main.storyboard 呈现 MainNavigationController 后,不会维护其 View 层次结构。
我的意思是,原本应该位于另一个全屏 UIView 之上的标签和按钮(但不是它的 subview ,因此它们应该保留)现在位于它的后面。
可能导致此问题的原因以及使它们出现在顶部的最简单方法是什么(就像我打开 main.storyboard 时它们所做的那样)
编辑
我在MainNavigationController的rootView的viewDidLoad方法中添加了一行代码:
self.view.sendSubviewToBack(wholeScreenView)
它解决了问题。
但是,有人知道为什么我必须自己编写代码并且 View 与 Main.storyboard 中的 View 不同吗?
最佳答案
Storyboard的配置方式与您想象的不同。您的 wholeScreenView
实际上位于 Storyboard中其他 View 的前面。其他 View (标签和按钮)不是 WholeScreenView 的 subview ;它们是主视图的 subview ,wholeScreenView
也是如此。它是后面的 subview ,因此位于它们的前面。
关于ios - 从新的 Storyboard实例化 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37365160/