【问题标题】:MyScript how to add mathwidget as a subview inside another UIViewControllerMyScript 如何将 mathwidget 添加为另一个 UIViewController 中的子视图
【发布时间】:2017-11-02 03:40:05
【问题描述】:

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

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

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

谁能帮助将 MathWidget 显示为当前 UIViewController 中的子视图?

【问题讨论】:

  • MathWidgetClassNameUIVIewController 的子类?

标签: ios swift uiview uiviewcontroller subview


【解决方案1】:

您正在以编程方式创建视图控制器,然后您需要设置它的框架和背景颜色,

    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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    相关资源
    最近更新 更多