どうも、ちょげ(@chogetarou)です。
Pickerをユーザーが操作できないようにする方法を紹介します。
方法
Pickerを操作できないようにするには、disabled修飾子を使います。
まず、Pickerにdisabled修飾子を付与し、disabledの引数に「true」を指定します。
Picker(・・・)
.disabled(true)
disabled修飾子を使うことで、Pickerを操作不可にすることができます。
使用例
struct ContentView: View {
@State private var selectedIndex = 0
@State private var isDisabled = false
var body: some View {
VStack {
Picker(selection: $selectedIndex, label: Text("Select").foregroundColor(.red)) {
ForEach(0..<3) {
Text("Item \($0)")
}
}
.pickerStyle(WheelPickerStyle())
.disabled(isDisabled)
Button("Switch") {
self.isDisabled.toggle()
}
}
}
}
コメント