Hi, I am trying to read in which section in a list the user is currently located and want to scroll him to a specific section. Therefore I would like to use the new .scrollPosition modifier. Best would be the ability to implement the same snapping effect from ScrollView.
So I used this snippet:
struct Item: Identifiable{
var id: Int
static func getMany() -> [Item] {
var items = [Item]()
for i in 0..<100 {
items.append(Item(id: i))
}
return items
}
}
struct ContentView: View {
@State var items = Item.getMany()
@State var scrolledID: Item.ID?
var body: some View {
NavigationStack {
List {
ForEach(items) { item in
ItemView(item: item)
}
}
.scrollTargetLayout()
.scrollPosition(id: $scrolledID)
.navigationTitle("Hello, \(scrolledID ?? 0)")
}
}
}
struct ItemView: View {
var item: Item
var body: some View {
Text("Hello world, \(item)")
}
}
Doesn't work.
So I tried to place the modifiers in different places in the code to attack several different parts of the list as the "scrollTargetLayout" - but this doesn't change anything here.
Isn't the List View just the Form inside a ScrollView?! This doesn't work either. If I place the Form OR List inside a ScrollView, the contents of the list aren't displayed anymore. This seems logical, because the list is a LazyVStack rendering without a height, as it doesn't know its final height. Can we fix this somehow?