Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust the main screen brightness using Swift

I would like to adjust the iPhone's main screen brightness in code using Swift.

I know in Objective-C it can be done by:

[[UIScreen mainScreen] setBrightness:0.5];

How do I do this in Swift?

like image 542
CircuitPro Avatar asked Jun 17 '14 13:06

CircuitPro


People also ask

What is the shortcut key to adjust brightness?

The Fn key is usually located to the left of the spacebar. The brightness function keys may be located at the top of your keyboard, or on your arrow keys. For example, on the Dell XPS laptop keyboard (pictured below), hold the Fn key and press F11 or F12 to adjust the brightness of the screen.


2 Answers

https://developer.apple.com/documentation/uikit/uiscreen/1617830-brightness

From the docs the proper answer for Swift 3+ is:

UIScreen.main.brightness = CGFloat(0.5)
like image 142
stride Avatar answered Sep 17 '22 12:09

stride


Actually in Swift 3 mainScreen was replaced with main, so proper code is:

UIScreen.main.brightness = CGFloat(0.5)
like image 21
Mykyta Savchuk Avatar answered Sep 19 '22 12:09

Mykyta Savchuk