apptekstudios / ASCollectionView

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

Workaround: Large navigation title not collapsing as expected #132

Open apptekstudios opened 4 years ago

apptekstudios commented 4 years ago

Describe the bug When using ASCollectionView with a large navigation title, there is a remaining blank space between NavigationBar and ASCollectionView after scrolling down.

This is a SwiftUI bug where the safe area is not being updated correctly.

Workaround Add .edgesIgnoringSafeArea(.all) and .alwaysBounce(vertical: true) to your ASCollectionView. The title should then collapse as expected.

Notes Previous related issues: #112, #125

I used the view debugger and confirmed that SwiftUI isn't propagating the safeArea change properly. Bug is happening at the level of UIHostingView which is unfortunately a private class... However ASCollectionView does respect the safeArea, so just tell SwiftUI to ignore safeAreas and our code will step in đź‘Ť

timohausmann commented 4 years ago

Thank you, .edgesIgnoringSafeArea(.top) after .layout() is enough to fix the issue for me. I'm testing it on BasicExample from the README inside a NavigationView inside a TabView.

.alwaysBounce(vertical: true) give me errors: Value of type 'ASCollectionView<Int>' has no member 'alwaysBounce' (or if I put it after .layout(): Value of type 'some View' has no member 'alwaysBounce'). (SwiftUI, XCode 11.5)

skunkworker commented 3 years ago

I ran into this issue, and it looks like tryin to append .background(Color(.systemGroupedBackground)) to the ASCollectionView caused the navigation title text to float in place and the title would never collapse.

Screen Shot 2020-10-03 at 2 35 34 PM

Is there a certain order that I could add the background color without causing that issue?

ASCollectionView{}.
.layout(self.layout)
.contentInsets(.init(top: 20, left: 0, bottom: 20, right: 0))
.alwaysBounceVertical()
.navigationBarTitle("My Title", displayMode: .automatic)
.edgesIgnoringSafeArea(.all)
apptekstudios commented 3 years ago

Hi @skunkworker, I haven’t had a chance to release the new version yet but someone else encountered this issue and made a simple fix in this PR (setting the background Color on the collectionview rather than using swiftUI’s modifier) https://github.com/apptekstudios/ASCollectionView/pull/173

skunkworker commented 3 years ago

Hi @skunkworker, I haven’t had a chance to release the new version yet but someone else encountered this issue and made a simple fix in this PR (setting the background Color on the collectionview rather than using swiftUI’s modifier) #173

Thanks @apptekstudios, I was able to pull the latest commit on v1.8.0WIP but the issue is still persisting.

Here is the basic example but the color doesn't work

struct ContentView: View {
    @State var dataExample = (0 ..< 30).map { $0 }

    var body: some View {
        NavigationView {
            ASCollectionView(data: dataExample, dataID: \.self) { item, _ in
                Color.blue
                    .overlay(Text("\(item)"))
            }
            .layout {
                .grid(layoutMode: .adaptive(withMinItemSize: 100),
                      itemSpacing: 5,
                      lineSpacing: 5,
                      itemSize: .absolute(50))
            }
            .backgroundColor(.red)
            .alwaysBounceVertical()
            .navigationTitle("Foobar")
            .edgesIgnoringSafeArea(.all)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
skunkworker commented 3 years ago

@apptekstudios it looks like there may have been a bad merge. I tracked down the PR here https://github.com/apptekstudios/ASCollectionView/commit/484a82d0e7d16359d593a2ff00a2b46dceb7d467#diff-7f9f29f1f24c322ca3c619f169098e877fa4e3df56da7732e282e3d4ec06857fL182

and for some reason the assignIfChanged(collectionView, \.backgroundColor, newValue: parent.backgroundColor) line is removed.

I have confirmed that this fixes the navigation bar issue though it does require a UIColor and not a SwiftUI Color object. And here is a one line PR to fix that removal https://github.com/apptekstudios/ASCollectionView/pull/187