How to present a View above everything in SwiftUI?

Hello,

I'm trying to present an app overlay (like an HUD) that should appear on top of everything (the app UI could be the root content view or a modal presented from the content view or a modal over a modal presented from the content view, etc.). If I use a ZStack for example, the issue is that the view is not visible if the ZStack is presenting a modal for example. In UIKit, I think we can use instantiate another UIWindow to show content above the top window of the app (what a native alert does if I'm not wrong).

What would be the equivalent in SwiftUI? How could I create this?

Thanks,

Axel

Hello @alpennec,

There might be a few ways to achieve the visual effect you're looking for.

Try using an .overlay or share your approach with ZStack below, provide a bit more details about your expected goal.

You also might want to get started with the Human Interface Guidelines and the iOS Pathway for beginner tutorials.

There's also great examples in our sample code Wishlist.

 Travis

Hello @DTS Engineer,

Thank you for answering.

I'll try to explain a little bit more the end result I want.

Basically, something like what Apple uses for the "Silent Mode" floating pill/indicator at the top of the screen. Something I could use in my app to indicate success/errors/confirmations when some actions are performed by the user.

Let say an action is performed in the SheetContentView, I want to immediately present the indicator while also dismissing the sheet. I dismiss the sheet I want the PillNotificationView to stay visible, on top of everything. So it can't be an overlay/ZStack to the SheetContentView, and it can't be an overlay/ZStack to the MyView.

Regarding the code (this does not work as explained above because the PillNotificationView is overlaid to the MyView due to the ZStack)

ZStack {
    MyView()

    if isPillPresented {
       PillNotificationView()
    }
}
.sheet(isPresented: $isPresentingSheet) {
    SheetContentView()
}

I want the view to be presented only once either above everything (no matter if the top view is the SheetContentView or MyView).

I hope it makes it more clear. Let me know.

Axel

PS:

How to present a View above everything in SwiftUI?
 
 
Q