如何将 mathwidget 添加为另一个 UIViewController 中的 subview 目前,mathwidget 在加载 UIViewController 时工作正常。 让 subViewEE = MathWidgetClassName() self.present(subViewEE, 动画: true, 完成: nil)
但是当我尝试将其添加为当前 View Controller 内的 subview 时,没有显示任何内容,代码如下:
let mathWidget= MathWidgetClassName()
self.addChildViewController(mathWidget)
self.view.addSubview(mathWidget.view)
mathWidget.didMove(toParentViewController: self)
任何人都可以帮助将 MathWidget 显示为当前 UIViewController 中的 subview 吗?
最佳答案
您正在以编程方式创建 View Controller ,然后您需要设置它的框架和背景颜色,
let mathWidget = MathWidgetClassName()
mathWidget.view.bounds = self.view.bounds
mathWidget.view.backgroundColor = UIColor.green // you should set white here , it is for demonstration
self.addChildViewController(mathWidget)
self.view.addSubview(mathWidget.view)
mathWidget.didMove(toParentViewController: self)
如果 Storyboard中有 View Controller ,那么您应该这样做,
let mathWidget = self.storyboard?.instantiateViewController(withIdentifier: "storyBoardID") //storyBoardID is Storyboard id - can be set from identity inspector of storyboard
// mathWidget?.view.bounds = self.view.bounds
// mathWidget?.view.backgroundColor = UIColor.green
self.addChildViewController(mathWidget!)
self.view.addSubview((mathWidget?.view)!)
mathWidget?.didMove(toParentViewController: self)
关于ios - MyScript 如何将 mathwidget 添加为另一个 UIViewController 中的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44299921/