我正在尝试创建一个简单的 Xcode 应用程序,按下按钮即可滑出侧面板。我知道有很多教程,但由于各种原因,我没有找到任何可以满足我特别需要的教程。 我尝试采取的第一步是在按下按钮时弹出一个新 View (完全位于我的第一个 VC 之上),但我遇到了问题。
按下按钮时将调用以下函数:
let otherVC = OtherViewController()
self.view.insertSubview(otherVC.view, at: 0)
self.view.bringSubviewToFront(otherVC.view)
OtherViewController 是在 Storyboard 中设置的,我知道它的 viewDidLoad 函数是用上面的代码调用的......我只是不明白为什么完全没有任何反应,并且另一个 VC 没有出现。我还应该做些什么来达到我想要的结果吗?
最佳答案
您不能像任何其他对象一样通过启动来调用 View Controller , Storyboard中的 View 不会附加到它
相反,
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let otherVC = storyboard.instantiateViewController(withIdentifier: "OtherViewController") as! OtherViewController
除此之外,您还必须为 StoryBoard 中的 viewController 分配一个 Storyboard Id。
You have to add the Storyboard ID here
之后,您应该查看 UIView Containment 以获得进一步的帮助
关于swift - 添加 subview 并置于前面,但仍然不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554308/