23

How can I change the brightness of the screen programmatically using iPhone SDK?

1

3 Answers 3

54
[[UIScreen mainScreen] setBrightness: yourvalue];

Requires iOS 5.0 or later. yourvalue is a float between 0.0 and 1.0.

5
  • Glad I could help. You can accept the answer if it is what you were looking for :) Jan 20, 2012 at 5:20
  • Wouldn't this get you kicked off the App Store though? May 5, 2012 at 21:44
  • It's a perfectly legal API call. I can't see a reason why there should be any problem with it. May 21, 2012 at 11:53
  • 1
    @Robse you may store the original brightness using UIScreen.mainScreen().brightness first then restore the brightness using this value at a later stage.
    – chengsam
    Aug 8, 2016 at 6:32
  • I add this line [[UIScreen mainScreen] setBrightness: 0.6f]; to viewDidAppear and nothing happened.
    – Jared Chu
    Oct 31, 2016 at 3:50
14

UPDATE: For Swift 3

UIScreen.main.brightness = YourBrightnessValue

Here's the swift answer to perform this

UIScreen.mainScreen().brightness = YourBrightnessValue

YourBrightnessValue is a float between 0.0 and 1.0

1
  • 1
    to request current brightness: let brillo : CGFloat = UIScreen.main.brightness Nov 2, 2017 at 14:49
2

I had some problems with changing the screen brightness in viewDidLoad/viewWillDisappear so I created a singleton class to handle all the action. This is how I do it:

import Foundation
import UIKit

final class ScreenBrightnessHelper {

    private var timer: Timer?
    private var brightness: CGFloat?
    private var isBrighteningScreen = false
    private var isDarkeningScreen = false

    private init() { }

    static let shared = ScreenBrightnessHelper()

    func brightenDisplay() {
        resetTimer()
        isBrighteningScreen = true
        if #available(iOS 10.0, *), timer == nil {
            brightness = UIScreen.main.brightness
            timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { (timer) in
                UIScreen.main.brightness = UIScreen.main.brightness + 0.01
                if UIScreen.main.brightness > 0.99 || !self.isBrighteningScreen {
                    self.resetTimer()
                }
            }
        }
        timer?.fire()
    }

    func darkenDisplay() {
        resetTimer()
        isDarkeningScreen = true
        guard let brightness = brightness else {
            return
        }
        if #available(iOS 10.0, *), timer == nil {
            timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { (timer) in
                UIScreen.main.brightness = UIScreen.main.brightness - 0.01

                if UIScreen.main.brightness < brightness || !self.isDarkeningScreen {
                    self.resetTimer()
                    self.brightness = nil
                }
            }
            timer?.fire()
        }
    }

    private func resetTimer() {
        timer?.invalidate()
        timer = nil
        isBrighteningScreen = false
        isDarkeningScreen = false
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.