Swift frame修飾子を使ってセグメントピッカーの横幅を変えるサンプル

環境
Swift version 5.2.3 (swift-5.2.3-RELEASE)

構文
Picker(xxx)
.pickerStyle(SegmentedPickerStyle())
.frame(width : width) //widthに横幅を指定

Pickerにframe修飾子を付与します。
frameの引数「width」に横幅を指定します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
struct ContentView: View {
@State private var selectedIndex = 0
var body: some View {
VStack {
Picker(selection: $selectedIndex, label: Text("Select").foregroundColor(.blue)) {
ForEach(0..<3) {
Text("Prod \($0)")
}
}
.pickerStyle(SegmentedPickerStyle())
.frame(width : 200)
}
}
}
struct ContentView: View { @State private var selectedIndex = 0 var body: some View { VStack { Picker(selection: $selectedIndex, label: Text("Select").foregroundColor(.blue)) { ForEach(0..<3) { Text("Prod \($0)") } } .pickerStyle(SegmentedPickerStyle()) .frame(width : 200) } } }
struct ContentView: View {
    
    @State private var selectedIndex = 0
    
    var body: some View {
        VStack {
            
            Picker(selection: $selectedIndex, label: Text("Select").foregroundColor(.blue)) {
                ForEach(0..<3) {
                    Text("Prod \($0)")
                }
            }
            .pickerStyle(SegmentedPickerStyle())
            .frame(width : 200)
            
        }
    }
}

 

IT

Posted by arkgame