I am trying to put a List into ScrollView.
If you want to know why, here is my explanation, if not - just skip to the code piece. I need to have a smoothly scaling header on top my List. In order to do that, I should put both the header and the List onto a ScrollView and do the rest using scroll content offset. I also need to forbid List's built-in scroll, since it is already on a ScrollView, and fix the height for the List - so the scroll view knows its content height. It works great, but the height of the List is sometimes(!) wrong.
I can't use a LazyVStack instead of a List - it is slower (no cell reuse I guess), and the List is big. It also breaks NavigationLinks, so it's out - it has to be a List.
The height of cells isn't fixed, so I can't just calculate manually.
Here is my code to create an unscrollable List with fixed height, using UITableView:
struct AutosizingList<Content: View>: View {
@ViewBuilder var content: Content
@State private var tableContentHeight: CGFloat = 0
var body: some View {
List {
content
}
.introspectTableView { tableView in
tableView.isScrollEnabled = false
tableContentHeight = tableView.contentSize.height
}
.frame(height: tableContentHeight)
}
}
I guess this block is not always called after didLayoutSubviews or something, and that's why the height is sometimes correct and sometimes wrong. So here is my question: how do I reliably get List's height? Can I use something remotely analogous to didLayoutSubviews in SwiftUI?