apptekstudios / ASCollectionView

A SwiftUI collection view with support for custom layouts, preloading, and more.
MIT License
1.36k stars 160 forks source link

Collection view hidden when embedded in ScrollView #152

Closed joeycarmello closed 4 years ago

joeycarmello commented 4 years ago

Describe the bug

I'm attempting to use the tags example from the demo in a simple ScrollView. The demo renders successfully without the ScrollView, but renders nothing at all when embedded within a ScrollView. I've tried variations of settings explicit frames on the collection view and items.

To Reproduce

Minimal example preview:

struct TagExample_Previews: PreviewProvider {
    struct DataItem: Identifiable {
        let id = UUID()
        let name: String
    }

    static var previews: some View { 
        let data: [DataItem] = [
            .init(name: "one"),
            .init(name: "second"),
            .init(name: "three"),
            .init(name: "four"),
        ]

        return Group {
            ScrollView {
                VStack {
                    ASCollectionView(
                        section: ASCollectionViewSection(
                            id: 0,
                            data: data,
                            contentBuilder: { (item, _) in
                                Text(item.name)
                        })
                        .selfSizingConfig { _ in
                            ASSelfSizingConfig(canExceedCollectionWidth: false)
                        }
                    )
                    .layout {
                        let fl = AlignedFlowLayout()
                        fl.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
                        return fl
                    }
                    .shrinkToContentSize(isEnabled: true, dimension: .vertical)
                }
            }
        }
    }
}

Expected behaviour

The collection view should render correctly within a ScrollView using self-sizing items.

Screenshots

Without ScrollView:

Screen Shot 2020-06-03 at 11 36 17 AM

Xcode Version:

Simulator, Device, Both?

apptekstudios commented 4 years ago

Thanks for reporting the bug @joeycarmello - this is a regression of an old bug in SwiftUI, so I have reinstated an old workaround in 1.7.1. Should now be working 🎉

Bug: SwiftUI isn't calling viewDidAppear when we nest a UIViewControllerRepresentable inside a scrollView.

joeycarmello commented 4 years ago

Awesome. I can confirm 1.7.1 fixed it. Thank you!