Is there a way of adjusting the screen brightness programmatically?

后端 未结 3 1742
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 10:28

I have an iPhone app for photographic purposes (kind of a lightbox). This app needs as much brightness as possible. Is there a way to change the screen brightness programmat

3条回答
  •  孤街浪徒
    2020-11-28 10:48

    Here's a Swift answer to this question.

    import UIKit
    extension UIScreen
    {
        static func setMainBrightness(brightness: CGFloat)
        {
            guard (0...1).contains(brightness) else
            {
                print("Attempt to set the screen brightness to an invalid value: \(brightness) should be between 0 and 1 inclusive.")
                return
            }
            self.main.brightness = brightness
        }
    }
    

    Call it by using:

    UIScreen.setMainBrightness(0.5)
    

    Or ignore my extension (which I just wrote to illustrate the limits) and just call:

    UIScreen.main.brightness = 0.5
    

提交回复
热议问题