apptekstudios / ASCollectionView

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

Background colors applied using SwiftUI's `background(_:alignment:)` modifier don't behave as expected #171

Open atdrendel opened 4 years ago

atdrendel commented 4 years ago

Describe the bug Trying to set a background color for an ASCollectionView nested inside of a NavigationView causes two problems:

  1. The background does not extend under the navigation bar.
  2. Large navigation titles do not collapse when scrolling the collection view.

To Reproduce Inside the demo app, modify the line in MainView.swift that instantiates PhotoGridScreen to look like this:

NavigationLink(destination: PhotoGridScreen().background(Color.green))

Expected behaviour The green background color should extend under the navigation bar, and the large navigation title should collapse when scrolling the collection view.

Screenshots IMG_0020

IMG_0021

Xcode Version: Xcode 11.6, but I'm pretty certain it'll happen on any version of Xcode that can build ASCollectionView.

Simulator, Device, Both? The problem occurs on both the simulator and device.

atdrendel commented 4 years ago

This is related to https://github.com/apptekstudios/ASCollectionView/issues/112. That issue seemed to have closed without a resolution, though.

apptekstudios commented 4 years ago

Thanks for the PR. I'll look into merging this into the upcoming version :)

It seems that the SwiftUI .background modifier is causing the issue. I managed to reproduce a similar issue when using a plain UIScrollView wrapped in SwiftUI. I'm curious whether using .edgesIgnoringSafeArea(.all) made any difference?

atdrendel commented 4 years ago

Thanks a lot for merging it.

I changed the PhotoGridScreen initialization line in MainView.swift to the following:

NavigationLink(destination: PhotoGridScreen().background(Color.green).edgesIgnoringSafeArea(.all))

Unfortunately, this approach doesn't seem to be workable, either. It creates a few new problems. First, the navigation bar becomes completely translucent. Second, the collection view's insets get screwed up, too, which leads to the content being scrolled up past the top of the screen on initial appearance. I'll include a screenshot below, but, overall, it seems like using the native UICollectionView.backgroundColor property is still the best approach to take.

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-09-05 at 15 41 36