Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI List row's not resising to fit contents

I have a List View with 1) a header view, 2) dynamic ForEach views, and 3) a footer view. My issue is that the first row, in which the header view lies, won't resise to fit its contents. The code for the main view is below:

var body: some View {
    List {
        GeometryReader { geometry in
            self.headerView(size: geometry.size)
        }
        ForEach(self.user.posts, id: \.self) { post in
            Text(post.title)
        }
        Text("No more posts...")
            .font(.footnote)
    }
    .edgesIgnoringSafeArea(.all)
}

This is the view which I am trying to achieve:

mockup

This is what I have so far...:

actual hierarchy

If it's any consolidation, the header view displays fine if it's outside of the list, however, that's not the view I'm looking for.

Thanks in advance.

P.S: Apologies for the huge images, I'm not sure how to make them appear as thumbnails...

like image 357
JacobCXDev Avatar asked Oct 29 '25 05:10

JacobCXDev


1 Answers

I prefer to use Sections for similar purposes (sections allow to have different configuration of each), like

enter image description here

var body: some View {
    List {
        Section {
           // << header view is here with own size
        }
        .listRowInsets(EdgeInsets()) // << to zero padding

        Section { // << dynamic view is here with own settings
            ForEach(self.user.posts, id: \.self) { post in
                Text(post.title)
            }
        }
        Section { // footer view is here with own size
            Text("No more posts...")
               .font(.footnote)
        }
    }
    .edgesIgnoringSafeArea(.all)
}
like image 97
Asperi Avatar answered Oct 31 '25 09:10

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!