When using a List in SwiftUI, expanding row items using anything other than the default DisclosureGroupStyle seems to cause a "bounce" during expansion/collapse.
What is the recommended way to avoid this bounce?
Link to code: https://github.com/dannys42-bugs/BugListExpansion (includes a small video demonstrating the issue)
@dannys42 This is not a bug. DisclosureGroup is a default built-in disclosure control provided by SwiftUI.
Depending on the context it's used whether it being within a List, a Stack, or side could behave and animate differently.
In your custom disclosure group style, create a Transaction
and set animation = nil
to disable the animation.
For example:
struct MyDisclosureStyle: DisclosureGroupStyle {
func makeBody(configuration: Configuration) -> some View {
VStack {
Button {
var transition = Transaction(animation: .snappy)
transition.animation = nil
withTransaction(transition) {
configuration.isExpanded.toggle()
}
} label: {
HStack(alignment: .firstTextBaseline) {
configuration.label
Spacer()
Text(configuration.isExpanded ? "hide" : "show")
.animation(nil, value: configuration.isExpanded)
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
if configuration.isExpanded {
configuration.content
}
}
}
}