0
votes

how to add mathwidget as a subview inside another UIViewController Currently, mathwidget is working fine when loading UIViewController. let subViewEE = MathWidgetClassName() self.present(subViewEE, animated: true, completion: nil)

But when I am trying to add it as a subview inside present view controller nothing shows up, here is the code:

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

Can anyone help to display MathWidget as a subview in present UIViewController?

1
MathWidgetClassName is sub class of UIVIewController ?Ketan Parmar

1 Answers

0
votes

you are creating viewcontroller programmatically then you need to set frame and background color of it like,

    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)

If you have view controller in storyboard then you should do like,

   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)