Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Screen Brightness

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.

like image 225
Z-Dog Avatar asked Nov 20 '16 23:11

Z-Dog


People also ask

How can I get current brightness?

To get the brightness of a screen use the getint() method that returns the current brightness of a system. You will the brightness to the setProgress() method as an argument. Now set the seekbar on setOnSeekBarChangeListener() to change the brightness of a screen.


2 Answers

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
like image 54
Ryan Collins Avatar answered Oct 25 '22 10:10

Ryan Collins


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

like image 21
alexringo Avatar answered Oct 25 '22 11:10

alexringo