ios - MyScript 如何将 mathwidget 添加为另一个 UIViewController 中的 subview

标签 ios swift uiview uiviewcontroller subview

如何将 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/

相关文章:

ios - 选择 UITableViewCell 内的图像

iphone - UIScrollView背后如何与UIView交互

ios - UIView 在运行时不会改变

ios - iPhone 6/iOS 8.3 屏幕尺寸错误?

ios - 付款队列 : updatedTransactions: not called

swift - 从 Firebase Firestore - Swift 4 获取当前用户的值(value)

ios - 带有 block 的 UIView 动画是如何工作的

ios - 如何在编辑模式下(在 UITableView 中)执行不同的 segue?

ios - UINavigationController 内的 iPhone UITabBarController?

ios - 将 subview 添加到 ScrollView 时遇到问题