notsobigcompany / BigUIPaging

A collection of SwiftUI views for handling pages of content
252 stars 14 forks source link

Allow SwiftUI-defined backgrounds behind PageView with .scroll style #2

Closed daprice closed 11 months ago

daprice commented 11 months ago

With .pageViewStyle(.scroll) I couldn’t set a background for the overall view in SwiftUI, only for each individual page’s content. Viewing the UI hierarchy in Xcode showed the ContentHostingController being stuck with the default white background. This PR sets the hosting controller’s view background color to .clear if the style is .scroll, allowing any background set in SwiftUI to show through.

I did this by adding a customBackgroundColor property to PlatformPageViewConfiguration, then passing the configuration all the way to the ContainerViewController so it can be referenced when creating the hosting controller. I’m not sure this is the best approach architecturally, but this way allows each PageViewStyle to define its own background color if needed (I found that setting a clear background broke the .book style and was only needed for .scroll), and doesn’t break API compatibility as far as I can tell.

daprice commented 11 months ago

The Page View With Navigation Stack example demonstrates the issue if you remove the background from the ContentView and add a non-default background to the RootView:

The issue:

Screenshot 2023-10-06 at 6 41 15 PM

With this PR:

Screenshot 2023-10-06 at 6 40 40 PM
phillipcaudell commented 11 months ago

Thanks for the PR @daprice!

Whilst setting the background on the page's content view with ignoreSafeArea will achieve the same effect as your example, I can imagine there are also scenarios where you do just want the page to be transparent.

A few thoughts:

To workaround this I suggest we mirror something akin to List's scrollContentBackground modifier which allows the behaviour to be toggled on/off.

I've gone ahead and added a new modifier on the main branch:

.pageContentBackground(.hidden)

This allows the parent background to punch through when needed whilst also preserving the default behaviour. It also has the benefit of allowing custom styles to adapt too.

I'll close this PR, but please let me know if you think I've missed anything.