Skip to content

Popup background appears but content is missing when scenePhase is observed in App Description #204

@anjiuzhe

Description

@anjiuzhe

When using Mijick/Popups in a SwiftUI app, the popup background (dim layer) appears, but the popup content view is not rendered.

the issue occurs only when @Environment(.scenePhase) is declared in the App struct.

@main
struct MyApp: App {
    @Environment(\.scenePhase) private var scenePhase   // <- causes the issue
    var body: some Scene {
        WindowGroup {
            RootView().registerPopups()
        }
    }
}
struct RootView: View {
    var body: some View {
        Text("Hello")
            .onAppear {
                ToastView.show("Hello")
            }
    }
}

Declaring @Environment(.scenePhase) in the App struct causes SwiftUI to rebuild the Scene / Window hierarchy during lifecycle changes.

Mijick/Popups attaches its popup host to the current window overlay.
When the Scene is rebuilt:
• the background layer may be recreated
• but the popup content’s hosting container is lost

This results in only the background being shown.

This is not obvious and very easy to trigger unintentionally.

Alternative: Use UIKit lifecycle notifications in App

@main
struct MyApp: App {
    init() {
        NotificationCenter.default.addObserver(
            forName: UIApplication.didEnterBackgroundNotification,
            object: nil,
            queue: .main
        ) { _ in
            print("Entered background")
        }
    }

    var body: some Scene {
        WindowGroup {
            RootView().registerPopups()
        }
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions