gpt4 book ai didi

swiftui - 在 SwiftUI 中使用 Toggle 禁用 Slider 会导致样式问题

转载 作者:行者123 更新时间:2023-12-05 00:55:21 26 4
gpt4 key购买 nike

使用切换来禁用/启用 slider 会导致样式反转,直到您与 slider 交互。显然我的做法是错误的!

struct ContentView: View {
@State var isActive = true
@State var value :Double = 2.0

var body: some View {
VStack {
Section(header:
HStack {
Toggle(isOn: $isActive)
{}.toggleStyle(SwitchToggleStyle(tint: .purple))
}
) {
HStack {
Slider(value: $value, in: 0...4, step: 1)
.accentColor(isActive ? .purple : .secondary).disabled(!isActive)
}.pickerStyle(SegmentedPickerStyle())
}
}
}
}

Initial state

Disable the toggle - note the Slider style is wrong, but the colour is correct

Enable the toggle again - note the Slider style is now disabled, and the colour (purple which is correct) is in the disabled style

如果您与重新启用的 slider 进行交互,样式会自行修复。

我希望我在这里做一些愚蠢的事情......

最佳答案

在末尾添加.id,如下所示。这会重置 slider ,它会根据需要显示。使用 Xcode 12.1/iOS 14.1 测试

Slider(value: $value, in: 0...4, step: 1)
.accentColor(isActive ? .purple : .secondary)
.disabled(!isActive)
.id(isActive) // << this !!

关于swiftui - 在 SwiftUI 中使用 Toggle 禁用 Slider 会导致样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64756306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com