gpt4 book ai didi

ios - 添加新的 Viewcontroller 的 View 作为 subview

转载 作者:可可西里 更新时间:2023-11-01 05:07:36 25 4
gpt4 key购买 nike

我正在尝试以下操作,但未能添加新的 viewcontrollers View 。它是呈现 View Controller 的唯一方法吗?我们不能从其他 Storyboard View Controller View 中添加 View 吗?

  //Working
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "customView") as! CustomViewController

self.present( viewcontroller , animated: true, completion: nil)

//Not working
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "customView") as! CustomViewController
vc.view.frame = self.view.frame
self.view.addSubview(vc.view)

最佳答案

您还需要在当前 Controller 中添加 CustomViewController 作为 ChildViewController

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "customView") as! CustomViewController
vc.view.frame = self.view.bounds
self.addChildViewController(vc)
self.view.addSubview(vc.view)
vc.didMove(toParentViewController: self) //OR vc.willMove(toParentViewController: self)

关于ios - 添加新的 Viewcontroller 的 View 作为 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42110341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com