概要
WWDC2019で発表されたSiwftUIをmacOS MojaveのPlaygroundで実行する方法の簡単なTipsです。
Xcode 11 Betaは以下URLからインストールしてください。
https://developer.apple.com/download/
コード例
以下のように、SwiftUIとPlaygroundSupportをimportし、PlaygroundPage.current.liveViewにUIHostingControllerをセットしてあげることでプレビューが可能となります。
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
VStack {
Text("SwiftUI")
.font(.title)
Text("in Swift Playground")
.font(.caption)
}
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
Tipsは以上です。以下のURLにSwigyUIの公式チュートリアルがあるので色々試してみると面白いかと思います。
Tutorials: https://developer.apple.com/tutorials/swiftui/
- [Creating and Combining Views] (https://developer.apple.com/tutorials/swiftui/creating-and-combining-views)
- Building Lists and Navigation
- Handling User Input
- Drawing Paths and Shapes
- Animating Views and Transitions
- Composing Complex Interfaces
- Working with UI Controls
- Interfacing with UIKit
Documentation: https://developer.apple.com/documentation/swiftui/