利用 Xcode playground 的 live view 顯示 App 畫面

Xcode playground 的 live view 可以幫我們顯示某個 controller 或某個 view 的畫面,讓我們不用啟動模擬器,就能在 App 畫面上操作進行互動,快速地實驗 App 的相關功能。

讓 playground 顯示 view 的程式如下。

import UIKit
import PlaygroundSupport

let image = UIImage(named: "winnie.png")
let imageView = UIImageView(image: image)
PlaygroundPage.current.liveView = imageView

若是沒看到右邊顯示圖片的區塊,可用以下方法顯示 Live View。

  • 從 Quick Actions 點選 Live View。
  • 點選下圖的框框,從選單點選 Live View。

讓 playground 顯示 controller 的程式如下。

import PlaygroundSupport
import UIKit

let controller = UIViewController()
controller.view.backgroundColor = .orange
PlaygroundPage.current.liveView = controller

以上程式的關鍵在於利用 PlaygroundPage.current.liveView 設定我們想要顯示的 controller 或 view。

--

--

彼得潘的 iOS App Neverland
彼得潘的 Swift iOS App 開發問題解答集

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com