quyle1222
quyle1222

Reputation: 11

Can't set UIScreen.main.brightness = 1.0

I setting UIScreen.main.brightness = 1.0 at viewDidLoad.

But print in console value of UIScreen.main.brightness is 0.9483038187026978.

Please help me. I not good English , so sorry

Upvotes: 0

Views: 408

Answers (1)

Arik Segal
Arik Segal

Reputation: 3031

You can use SceneDelegate as described in the following article:

https://medium.com/@arikisintown/how-to-create-an-ios-app-that-takes-secret-photos-while-the-iphone-screen-seems-to-be-turned-off-7081d110309e

--

From the article:

We will add a separate class dedicated to controlling the screen brightness:

final class DimUnDim {
    static let shared = DimUnDim()
    private var originalBrightness = UIScreen.main.brightness
    func dim() {
        print("dim")
        UIScreen.main.wantsSoftwareDimming = true
        UIScreen.main.brightness = 0.0
    }
    func unDim() {
        print("unDim")
        UIScreen.main.brightness = originalBrightness
    }
}

We will call Dim when the app becomes active and call UnDim when the app is not active. Edit the file SceneDelegate.swift and add the following:

func sceneDidBecomeActive(_ scene: UIScene) {
    DimUnDim.shared.dim()
}
func sceneWillResignActive(_ scene: UIScene) {     
    DimUnDim.shared.unDim()
}

Upvotes: -1

Related Questions