How can I change the brightness of the screen programmatically using iPhone SDK?
Asked
Active
Viewed 1.9k times
23
-
1Seeing as that is a part of official SDK, I suggest you check your sources. http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html – Peter Sarnowski Jan 20 '12 at 05:16
3 Answers
54
[[UIScreen mainScreen] setBrightness: yourvalue];
Requires iOS 5.0 or later. yourvalue is a float between 0.0 and 1.0.

Peter Sarnowski
- 11,900
- 5
- 36
- 33
-
Glad I could help. You can accept the answer if it is what you were looking for :) – Peter Sarnowski Jan 20 '12 at 05:20
-
-
It's a perfectly legal API call. I can't see a reason why there should be any problem with it. – Peter Sarnowski May 21 '12 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 08 '16 at 06:32
-
I add this line `[[UIScreen mainScreen] setBrightness: 0.6f];` to `viewDidAppear` and nothing happened. – Jared Chu Oct 31 '16 at 03: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

iYoung
- 3,596
- 3
- 32
- 59
-
1to request current brightness: let brillo : CGFloat = UIScreen.main.brightness – oscar castellon Nov 02 '17 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
}
}

nullforlife
- 1,354
- 2
- 18
- 30