MyScript如何在另一个UIViewController中添加mathwidget作为子视图

时间:2017-06-01 06:22:10

标签: ios swift uiview uiviewcontroller subview

如何在另一个UIViewController中添加mathwidget作为子视图 目前,mathwidget在加载UIViewController时工作正常。     让subViewEE = MathWidgetClassName()     self.present(subViewEE,animated:true,completion:nil)

但是当我试图将它作为子视图添加到当前视图控制器中时,没有任何显示,这里是代码:

let mathWidget= MathWidgetClassName()
self.addChildViewController(mathWidget) 
self.view.addSubview(mathWidget.view)
mathWidget.didMove(toParentViewController: self)

任何人都可以帮助在当前的UIViewController中将MathWidget显示为子视图吗?

1 个答案:

答案 0 :(得分:0)

您正在以编程方式创建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)