admin管理员组

文章数量:1125015

One of the screens in my app shows a barcode. When a user navigates to the screen (it is a tab in my tabview) I set the screen brightness to the maximum. When a user navigates away from the screen I restore the screen brightness to what it was. The brightness isn't restored when the user closes the app, either by moving it to the background or closing it completely. How can I restore the screen brightness when a user closes the app?

struct BarcodeView: View {
    @Environment(\.scenePhase) var scenePhase

    @State private var originalBrightness: CGFloat = 0.0

    var body: some View {
        Barcode()
            .onAppear {
                originalBrightness = UIScreen.main.brightness
                UIScreen.main.brightness = 1.0
            }
            .onDisappear {
                UIScreen.main.brightness = originalBrightness
            }
            .onChange(of: scenePhase) { _, newPhase in
                if newPhase == .background {
                    UIScreen.main.brightness = originalBrightness
                }
            }
    }
}

The .onDisappear and .onChange modifiers trigger when the app moves to the background or is closed, but the brightness is unchanged. From google I understand that this is intended behaviour, apps shouldn't be able to change the brightness settings when they aren't active, which makes sense. Is there a way to restore the original screen brightness or is this not possible?

本文标签: swiftuiHow to restore screen brightness when closing iOS appStack Overflow