0

I'm building a ViewModifier that looks like iOS alerts, except it's able to not have any buttons/actions on the popup. My main concern is that I'm using .overlay {} to show this alert on my content with a .opacity(isPresented ? 1 : 0) modifier. This alert would have a ProgressView() on it and I don't want SwiftUI to always be animating that ProgressView() behind the scenes.

I tried putting Color.black.opacity(isPresented ? 1 : 0).allowsHitTesting(false) on top of my content with .overlay and when the opacity was 0 it allowed me to tap any of the content that the Color.black would've been on top of, but when isPresented was true it didn't allow me to tap any of it. This seems like good enough evidence to me that it doesn't compute views with 0 opacities, but I was curious if anyone had any other insight.

joshgalv
  • 50
  • 5
  • SwiftUI from my experience will build views when opacity is 0 in addition to when the hidden modifier is added. You can test this with a text view where the text is returned from a function and having a print inside the function. However, when opacity is 0 the view will still take up "space" on the parent view, whereas hidden will not. Just out of curiosity -- why do you not want SwiftUI always animating the ProgressView behind the scenes? Is it rarely shown? – ZachtheBoB Jun 14 '23 at 16:45
  • @ZachtheBoB Yes it would be rarely shown. It's supposed to say "Loading `ProgressView()`". It ended up working with a conditional `content.overlay { if isPresented { Views(/*ProgressView() in here somewhere) } }`, instead of changing opacity. I had already tried this but it didn't work correctly with using Transitions (because I want to scale it too) so I tried to use ternary modifiers and then went back and it started working, not sure what I changed though. – joshgalv Jun 14 '23 at 19:50
  • Maybe this Stack Post and the WWDC video at the bottom of the answer, gives you a better understanding of it: https://stackoverflow.com/questions/56490250/dynamically-hiding-view-in-swiftui/57420479#57420479 – bennyyy999 Jun 15 '23 at 10:14
  • This is one of those occasions when using an `if` conditional in the view makes the most sense. – Yrb Jun 15 '23 at 16:56

0 Answers0