0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Playgroundで画面描画する方法

0
Posted at

概要

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()

表示

スクリーンショット 2021-09-23 22.16.43.png

おわりに

簡単なテストに是非。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?