1
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?

【SwiftUI 🔰】PickerとPickerStyleの使い方を説明してみました!

Posted at

SwiftUIのPickerは、ユーザーが選択肢の中から1つ(または複数)を選ぶためのUIパーツです。

Pickerとは?

Pickerは、iOSアプリで「ドロップダウンメニュー」「リスト選択」「セグメント選択」などを作るためのコンポーネントです。

基本の使い方



struct ContentView: View {
    @State var selectedColor = "赤"
    let colors = ["赤", "青", "緑"]

    var body: some View {
        VStack {
            Picker("色を選んでください", selection: $selectedColor) {
                ForEach(colors, id: \.self) { color in
                    Text(color)
                }
            }
            Text("選んだ色:\(selectedColor)")
        }
        .padding()
    }
}

Screenshot 2025-06-02 at 22.44.21.png

様々なPickerStyle

SwiftUIでは、PickerStyleを使ってPickerの表示スタイルを変えることができます。

1.wheel スタイル (良くアプリで見るスタイル)

Screenshot 2025-06-02 at 22.59.19.png

iPhoneのアラーム設定のようなUIになります。

2.segmented スタイル (タブ切り替え風)

Screenshot 2025-06-02 at 22.54.13.png

例:

struct ContentView: View {
    @State var selectedColor = ""
    let colors = ["🍎", "🍌", "🍇"]

    var body: some View {
        VStack {
            Text("好きなフルーツを選んでください")
                .font(.title2)

            Picker("フルーツ", selection: $selectedColor) {
                ForEach(colors, id: \.self) { color in
                    Text(color)
                }
            }
            .pickerStyle(SegmentedPickerStyle())

            Text("あなたが選んだフルーツ:\(selectedColor)")
                .padding(.top, 20)
        }
        .padding()
    }
}
  • → 横並びのボタンで簡単に切り替えられるスタイルです。

3.MenuPickerStyle (ポップアップスタイル)

Screenshot 2025-06-02 at 22.58.34.png

  • ラベルをタップするとメニューが開く形式。スペースを取らずに済みます。

まとめ

項目 内容
用途 選択肢の中から1つを選ぶUI
基本構文 Picker("ラベル", selection: $state) { ... }
スタイル .wheel, .segmented, .menuなどで見た目を変更可能
1
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
1
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?