Adjust the main screen brightness using Swift

CircuitPro picture CircuitPro · Jun 17, 2014 · Viewed 18.8k times · Source

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?

Answer

stride picture stride · Jun 17, 2014

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)