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?
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.
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)
Actually in Swift 3 mainScreen
was replaced with main
, so proper code is:
UIScreen.main.brightness = CGFloat(0.5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With