Wednesday, October 21, 2026
HomeiOS DevelopmentHow does .fullScreenCover work beneath the hood?

How does .fullScreenCover work beneath the hood?

[ad_1]

I am attempting to recreate a view modifier that acts like fullScreenCover. However I am unable to fairly determine it out. That is what I’ve received to this point:

extension View {
  public func foo(
    shouldPresent: Binding<Bool>
  ) -> some View {
    self.modifier(
      Bar(shouldPresent: shouldPresent)
    )
  }
}

struct Bar: ViewModifier {
  @Binding var shouldPresent: Bool

  func physique(content material: Content material) -> some View {
    content material
      .onReceive(Simply(shouldPresent)) { _ in
        updatePresentation(shouldPresent)
      }
  }

  non-public func updatePresentation(_ shouldPresent: Bool) {
    if shouldPresent {
      // Do presenting right here
    } else {
      // Do dismissing right here
    }
  }
}

That is then hooked up as a view modifier to a view like:

.foo(shouldPresent: $shouldPresent)

What I’ve discovered although is that if the state of the view that the modifier is hooked up to adjustments, such that it re-renders, the updatePresentation operate will get fired once more. This is not the meant behaviour as a result of it ought to solely get fired when the shouldPresent binding adjustments, not when any state within the view adjustments.
This results in sudden behaviour the place my presentation/dismissal code will get fired when it should not.

fullScreenCover does not appear to have this subject. How can I alter my code in order that it solely listens to the shouldPresent binding?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments