Propogate view specific information to view modifier?

Consider a view URLImage() which takes a path and downloads an image, and then displays it. Before the download is complete, the image will display some placeholder. So URLImage() basically calls to Image(), but picks, over time, what image to display based on whether the download has finished or not.

I'd like to apply a background only if the image is in the "displaying-placeholder state" (which URLImage() itself knows), but the background has to be applied after applying a frame modifier.

So:

  URLImage(someUrl).frame(width: 200, height: 180).background(XXX)

The issue is that XXX depends on what state URLImage() is in. I have no idea how to make a view modifier that could possibly peer into URLImage() to extract state. I would have URLImage() itself apply the background color, but it can't until it knows how big its frame is going to be.

How can that be done?

And the answer is... a preference key, to propogate this state up to any body above my URLImage() class that needs to query for it.

Thanks for the information keep sharing this type of information.

Terms of Service

Privacy Policy

Cookie Policy