How to change brightness in iOS 5 app?
Asked Answered
P

4

19

How would I program the ability to change brightness in-app? I know that its possible as I have seen at least three apps that can do it. This would be very useful for my app. I know that it's only possible in iOS 5 with the UIScreen Class, but I have no idea how to program it. Please help me!

Phylum answered 21/11, 2011 at 4:33 Comment(0)
S
35

The UIScreen class has a new property called brightness.

In addition, there's another property called wantsSoftwareDimming that (when set to YES) allows you to go below the lowest brightness supported by the hardware, because a special "dimming view" is overlaid over the screen to darken things even further.

The brightness property takes a float from 0 to 1. So:

  • with wantsSoftwareDimming set to NO (the default), a brightness of 0 means "the darkest the hardware supports" and a brightness of 1 means "the brightest the hardware supports".
  • with wantsSoftwareDimming set to YES, a brightness of 0 means "the darkest the hardware supports PLUS darkening by overlaying a dimming view", and a brightness of 1 still means "the brightest the hardware supports".
Simian answered 21/11, 2011 at 4:40 Comment(1)
Thanks for the information. I didn't know about the 'wantsSoftwareDimming' option, but it helps my app a lot!Phylum
S
13

I have never tried it, but looking at the docs it should go like this:

UIScreen *mainScreen = [UIScreen mainScreen];
mainScreen.brightness = .5; //should set the brightness at 50%
Sabinasabine answered 21/11, 2011 at 4:37 Comment(0)
R
1

You can use either of these two:

  1. [[UIScreen mainScreen]setBrightness:1.0];

  2. GSEventSetBacklightLevel(0.5f); But this is a private API call, if you use it, your application will surely be rejected.

Riant answered 21/11, 2011 at 4:40 Comment(0)
G
1

As others pointed out you can use

[[UIScreen mainScreen] setBrightness:1.0];

BUT be very careful because you will run into problems (talking from experience here) look at this: IOS5 setBrightness didn't work with applicationWillResignActive and this: Anyone been able to use [[UIScreen mainScreen] setBrightness] on background / exit?

(I wish I had, when I discovered this thread/answer) :-(

Gaslight answered 4/10, 2012 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.