3
2

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.

SwiftUIとりあえず試すまで

Posted at

Swift UIの体験の仕方

SwiftUIを体験したり、試すのに必要な手順です。

とりあえずアプリで使えるまで

  1. 新しいプロジェクトを作る

  2. Applicationを選択(Single View Appで良い)

  3. プロジェクトのオプションでLanguage: SwiftUserInterface: SwiftUIで選択する
    スクリーンショット 2020-09-12 16.39.38.png

  4. コードは既に生成されているので実行する。

ちなみにObjectiveCとSwiftUIの組み合わせは選択できません。

できた!

とりあえずPlaygroundで試せるようにするまで。

  1. Playgroundを開いて、以下を記入、実行する。
import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

// playgroundでの実行に必要
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())

できた!
スクリーンショット 2020-09-12 16.43.38.png

ツールの使い方。

Playgroundは基本的に実行ボタンを押すだけです。こちらでは主にXcodeでProjectを開発している時の使い方について書きます。

プレビュー機能

簡単にSwiftUIのコードを試せる機能があります。

右上にあるResumeボタンを押しましょう。
スクリーンショット 2020-09-12 16.51.06.png

この状態でコードを変更すると、UIのプレビューも同時に変わります。

GUIを利用してUIを修正する

Commandを押しながら、UI部品をクリックすると、メニューが出ます。
Show SwiftUI Inspectorを選択すると、その部品の操作が直感的に行えます。

Command + Clickでメニュー

スクリーンショット 2020-09-12 17.04.06.png

Inspector

スクリーンショット 2020-09-12 17.06.04.png

Inspector上で変更した項目は、SwiftUIのコード側にも自動で反映されます。

  • InspectorはUI部品を選択して、Command + Option + 4でも呼び出せます!
  • SwiftUIのコードのUI部品をCommand + Clickでもメニューは出せます。

まとめ

Swift UI是非試してみてください。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?