![](/img/trans.png)
[英]Add UIViewController inside a Subview of custom UITableViewCell
[英]MyScript how to add mathwidget as a subview inside another UIViewController
如何將mathwidget添加為另一個UIViewController內的子視圖當前,加載UIViewController時mathwidget可以正常工作。 讓subViewEE = MathWidgetClassName()self.present(subViewEE,動畫:true,完成:無)
但是,當我嘗試將其添加為當前視圖控制器內的子視圖時,什么都沒有顯示,這是代碼:
let mathWidget= MathWidgetClassName()
self.addChildViewController(mathWidget)
self.view.addSubview(mathWidget.view)
mathWidget.didMove(toParentViewController: self)
任何人都可以幫助將MathWidget顯示為當前UIViewController中的子視圖嗎?
您正在以編程方式創建viewcontroller,則需要設置它的框架和背景顏色,例如,
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)
如果您在情節提要中具有視圖控制器,則應該這樣做,
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.