-
-
Save helje5/0d8013dc40bf435b23d0074bce31af29 to your computer and use it in GitHub Desktop.
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() | |
} |
Wow this is embarrassing for the SwiftUI team. Quite ugly. I couldn't figure out a way to make it look substantially better :-/
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.
@robb came up w/ hack to workaround the padding issue: https://gist.github.com/robb/b262fe83baabace03cfd65581d1a01bd.
This works for me, if I wrap theLabel
in it:The magic numbers don't always work, seems to depend on the content.