概要
XcodeのPlaygroundで画面表示する方法です。
実行環境
- Apple Swift version 5.4
- Xcode 12.5
方法
UIKit,XCPlayground,PlaygroundSupportをインポートして、表示したいViewControllerをPlaygroundPage.current.liveViewに代入する。
以下のコードを貼り付けると動きます。
コード
MyPlayground.playground
import UIKit
import XCPlayground
import PlaygroundSupport
class ViewController: UIViewController{
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
self.view.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
self.label.text = "label"
self.label.frame = CGRect(x:0, y:300, width:370, height:30)
self.label.textAlignment = NSTextAlignment.center
self.view.addSubview(label)
}
}
PlaygroundPage.current.liveView = ViewController()
表示
おわりに
簡単なテストに是非。
