When building app using SwiftUI you can come across some limitations it has. One of such limitations is that we cannot open another view full screen using .sheet modifier.
It’s very sad. But here is workaround I found and would like to share with you!
Suppose we have tab view based application (MainView) and want to show some popup modally with dimmed background on one of its tabs (AccountView). The first thing that comes to mind is to use ZStack in AccountView using @State variable that triggers modal view visibility. That’s right thing. So let’s write the code down. That’s what we have.
But what do we get?
Yeah. That’s not fun. The problem is that we show ModalView in the tab’s hierarchy, not in MainView hierarchy.
The trick is to utilize @EnvironmentObject to notify MainView about the view we want to show full screen.
Let’s start with modifying MainView:
Now we have to modify our ModalView to use custom dismissing closure, because we now have to nullify content property of the provider to make view disappear:
And the last step is to use all of this to show your full screen modal:
That’s it! Do not forget to set environment object to MainView in SceneDelegate.
Hope I helped someone!