[英]SwiftUI: `Toggle`s within dynamic `List` breaking their layout upon reuse?
I'm trying to show a dynamic List
with rows containing Toggle
elements.我正在尝试显示包含
Toggle
元素的行的动态List
。 The Toggle
s are laid out correctly initially, but their layout breaks when scrolling them in and out of view (ie upon cell reuse). Toggle
最初的布局是正确的,但是当它们滚动进出视图时(即在单元格重用时),它们的布局会中断。
Minimal example code:最小示例代码:
import SwiftUI
struct SwitchList: View {
var body: some View {
List(0..<20) { _ in
SwitchRow(value: Bool.random())
}
}
}
struct SwitchRow: View {
@State var value: Bool
var body: some View {
Toggle(isOn: $value) {
Text("A switch row")
}
}
}
Screen recording demonstrating the issue:演示问题的屏幕录像:
(This is using iOS 13.2.2 (17B102) on the Simulator.) (这是在模拟器上使用 iOS 13.2.2 (17B102)。)
Am I doing something wrong, or is this a bug?我做错了什么,还是这是一个错误? How do I get the
Toggle
s to show correctly?如何让
Toggle
正确显示?
This is a bug/regression in iOS 13.2+这是 iOS 13.2+ 中的错误/回归
Working - iOS 13.1 (17A844) / Xcode 11.1 (11A1027)工作 - iOS 13.1 (17A844) / Xcode 11.1 (11A1027)
Broken - iOS 13.2.2 (17B102) / Xcode 11.2.1 (11B500)损坏 - iOS 13.2.2 (17B102) / Xcode 11.2.1 (11B500)
Broken - iOS 13.3 beta (17C5032d) / Xcode 11.3 beta (11C24b)损坏 - iOS 13.3 beta (17C5032d) / Xcode 11.3 beta (11C24b)
Submit feedback to Apple向 Apple 提交反馈
Workaround解决方法
This bug only appears to affect the List
initializers which take a data
parameter.此错误似乎仅影响采用
data
参数的List
初始化程序。 This code is functionally equivalent, but is not affected by the bug.此代码在功能上是等效的,但不受该错误的影响。
struct SwitchList: View {
var body: some View {
List {
ForEach(0..<20) { _ in
SwitchRow(value: Bool.random())
}
}
}
}
I was able to reproduce the problem, but could not find out why this happens.我能够重现该问题,但无法找出发生这种情况的原因。 When I use a
ScrollView()
with a Devider()
I don't have the Problem anymore.当我将
ScrollView()
与Devider()
) 一起使用时,我不再有问题了。 Here is the code:这是代码:
struct SwitchList: View {
var body: some View {
ScrollView {
ForEach(1...50, id: \.self) { item in
VStack {
SwitchRow(value: Bool.random())
Divider()
}
}
}
}
}
struct SwitchRow: View {
@State var value: Bool
var body: some View {
Toggle(isOn: $value) {
Text("A switch row")
}
}
}
I hope this helps!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.