23

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
CircuitPro
  • 401
  • 1
  • 3
  • 7

2 Answers2

59

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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
stride
  • 1,931
  • 1
  • 17
  • 20
28

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

UIScreen.main.brightness = CGFloat(0.5)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mykyta Savchuk
  • 1,195
  • 13
  • 13