Get Current Screen Brightness
Asked Answered
H

3

7

Does anyone know how to get the user's current screen brightness or temporarily change the brightness just for one view?

When the user launches a specific view the screen dims to the lowest brightness possible. In this case, I'm using:

UIScreen.mainScreen().brightness = CGFloat(0.0)

But I want to know that when the user dismisses the view the brightness returns to exactly what they had.

An app that uses this is Stocard. When viewing a card's barcode the screen's brightness brightens to the max and when the barcode view dismisses the screen's brightnes returns to exactly what the user had before.

If anyone knows how to do this in Swift 2.3 that would be great.

Heidy answered 20/11, 2016 at 23:12 Comment(0)
G
9

You're looking for the UIScreen class.

Just access the main screen and get the property like this:

UIScreen.mainScreen().brightness

i.e.

let currentBrightness = UIScreen.mainScreen().brightness

(You can also set the brightness property in addition to getting it)

In Swift 4+

let currentBrightness = UIScreen.main.brightness
Greenes answered 20/11, 2016 at 23:18 Comment(2)
He did ask for Swift 2.3 syntax. So that would be UIScreen.mainScreen().brightness. If that's not obvious to anyone.Venitavenite
let currentBrightness = UIScreen.main.brightness in Swift 4.1Shackleton
S
7

Get current screen brightness Swift 3

    let currentBrightness = UIScreen.main.brightness  

Set screen brightness Swift 3

    UIScreen.main.brightness = CGFloat(0.7)  

The range is 0.0 - 1.0 as mentioned in Apple's documentation

Salientian answered 11/1, 2018 at 3:27 Comment(0)
H
0

You will need to save the value you want to set yourself in your NSUserDefaults and call setBrightness: in your application delegate's applicationDidBecomeActive: method to restore the brightness.

Hedberg answered 21/11, 2016 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.