我正在尝试使用Swift操场制作一些简单的绘图代码的原型。从单视图iOS模板开始:
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
那里没有争议。我希望它包含获取字符串“ Hello World!”所需的一切。在屏幕上显示(否则,为什么要在模板中显示它?),但是如果我运行它,我看到的只是一个黑色矩形。
在注意到应用UIAnimation
足以使视图正确呈现之后,我进行了实验,发现对模板进行最小更改以使其实际绘制出的内容是:
// Present the view controller in the Live View window
let vc = MyViewController()
PlaygroundPage.current.liveView = vc
PlaygroundPage.current.needsIndefiniteExecution = true
vc.view.setNeedsDisplay()
我的设置是否不当或所提供的模板不足以产生可见的输出?