Adjust the main screen brightness using Swift
Asked Answered
L

2

24

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?

Luthern answered 17/6, 2014 at 13:7 Comment(0)
J
60

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)
Jody answered 17/6, 2014 at 13:14 Comment(0)
H
28

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

UIScreen.main.brightness = CGFloat(0.5)
Horrorstruck answered 20/9, 2016 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.