[SwiftUI]Picker(ピッカー)をセグメントにするには?

SwiftUI

どうも、ちょげ(@chogetarou)です。

Pickerの表示スタイルをセグメント(Segment)にする方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

Pickerの表示スタイルをセグメント式にするには、pickerStyle修飾子を使います。

まず、PickerにpickerStyle修飾子を付与します。

そして、pickerStyle修飾子の引数に「SegmentedPickerStyle」を指定します。

Picker(・・・)
    .pickerStyle(SegmentedPickerStyle())

pickerStyle修飾子を使うことで、Pickerをセグメントにすることができます。

使用例

struct ContentView: View {
    @State private var selectedIndex = 0
    
    var body: some View {
        VStack {
            
            Picker(selection: $selectedIndex, label: Text("Select").foregroundColor(.red)) {
                ForEach(0..<5) {
                    Text("Item \($0)")
                }
            }
            .pickerStyle(SegmentedPickerStyle())
        }
    }
}

コメント

タイトルとURLをコピーしました