是否可以禁用一半的步进器

sfu*_*ng3 2 swift swiftui

当步进器的值为零时,我想禁用一半的步进器。

我在步进器上尝试了 .disabled 函数,但它禁用了整个步进器,我只想禁用步进器的递减部分。

struct StepperLabelView : View {
    @ObservedObject var social: Social

    var body: some View {
        VStack {
            Stepper(onIncrement: {
                self.social.quantity += 1
                socialsInCanvas += [Social(companyName: self.social.companyName)]
            }, onDecrement: {
                self.social.quantity -= 1
                socialsInCanvas.removeLast()
            }, label: { Text("") })
                .disabled(social.quantity == 0)
        }
        .padding()
    }
}
Run Code Online (Sandbox Code Playgroud)

Moj*_*ini 5

Stepper可以采取范围来激活每个按钮:

struct ContentView : View {

    @State var quantity = 3

    var body: some View {
        VStack {
            Stepper("Count: \(quantity)", value: $quantity, in: 0...Int.max)
        }
        .padding()
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用onEditingChanged参数来添加额外的工作。您还可以观察quantity

@State var quantity = 3 {
    didSet {
        if oldValue < quantity {
            // Probably + button touched
            return
        }

        if oldValue > quantity {
            // Probably - button touched
            return
        }

        if oldValue == quantity {
            // Unknown
        }

    }
}
Run Code Online (Sandbox Code Playgroud)