首页 > 解决方案 > 如何在 macOS 的 Swift Playgrounds 中呈现 UIViewController?

问题描述

我在我的 macOS 设备上从 AppStore 下载了 Swift Playgrounds v3.2。我想在一个空项目中展示 UIViewController 类的一个实例。我试图像在 Xcode 操场上一样展示它,PlaygroundPage.current.liveView = vc但它不起作用。

import PlaygroundSupport

let vc = UIViewController()

PlaygroundPage.current.liveView = vc

PlaygroundPage.current.needsIndefiniteExecution = true

标签: iosswiftswift-playground

解决方案


确保您导入UIKit

然后像这样将 UIViewController 的视图设置为 liveView

PlaygroundPage.current.liveView = vc.view

推荐阅读