Skip to content

Instantly share code, notes, and snippets.

@helje5
Last active October 16, 2023 21:30
Show Gist options
  • Save helje5/0d8013dc40bf435b23d0074bce31af29 to your computer and use it in GitHub Desktop.
Save helje5/0d8013dc40bf435b23d0074bce31af29 to your computer and use it in GitHub Desktop.
ListCellSizingFlicker
import SwiftUI
struct Item: Identifiable {
let id = UUID()
let title = "Hello"
}
let items = [
Item(), Item(), Item(), Item(), Item(), Item(), Item(), Item(), Item(), Item(), Item(),
Item(), Item(), Item(), Item(), Item(), Item(), Item(),
]
struct ContentView: View {
@State private var animated = true
var body: some View {
NavigationStack {
Toggle("Animated", isOn: $animated).padding()
List {
ForEach(items) { item in
Cell(item: item, animated: animated)
}
}
}
}
struct Cell : View {
@State private var isExpanded = false
let item : Item
let animated : Bool
private func toggle() {
if animated {
withAnimation { isExpanded.toggle() }
}
else {
isExpanded.toggle()
}
}
var body: some View {
Label {
VStack {
HStack {
Text(verbatim: item.title)
Spacer()
Image(systemName: "battery.75percent")
}
.contentShape(Rectangle())
.onTapGesture(perform: toggle)
if isExpanded {
VStack {
Text("More")
Text("Info")
Text("on item")
}
}
}
} icon: {
Image(systemName: "lightswitch.on.square")
}
}
}
}
#Preview {
ContentView()
}
@helje5
Copy link
Author

helje5 commented Oct 16, 2023

I wouldn't call it embarrassing, it seems like a deficiency that should be addressed and it is sad that it still exists. I would even be OK to disable the slide out animation, but the offset issue is really bad.

One thing that can be done to make it work is to put the expanded content into own rows. But that has a lot of other side effects that just don't work well in my particular scenario.

Context: I do have an (4y?) old SwiftUI app in which I had to drop List originally because of such issues. I'm working on an update and thought I'd give List another try. Looks like I have to stick to a plain ScrollView even though I'd love to use the List for the extra features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment