为什么我不能设置UIScreen.main.brightness?
Why cannot I set UIScreen.main.brightness?
我刚开始制作一个新应用。代码 UIScreen.main.brightness = 1.0
在任何地方都不起作用。我在 viewDidLoad
中尝试过,在 viewWillAppear
中尝试过。我注释掉了其他所有内容,并使它成为唯一的 运行 代码行,但它不起作用。我无法改变亮度。我正在我的 iPhone X - iOS 13.4.1 真实设备上试用它。可能是什么原因?
编辑:还尝试了一个新的 Xcode 项目。还是不行。
我注意到在 iOS 13 中,从 viewDidLoad 和 viewWillAppear 调用 UIScreen.main.brightness 没有任何效果。将它添加到 viewDidAppear 工作正常。
它也可以很好地放入按钮操作。
import UIKit
class ViewController: UIViewController {
var brightness: CGFloat = 1.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let button = UIButton(type: .infoDark)
// This call is ignored
UIScreen.main.brightness = CGFloat(0.3)
view.addSubview(button)
button.frame = CGRect(x: 40.0, y: 100.0, width: button.frame.width, height: button.frame.height)
button.addTarget(self, action: #selector(toggle), for: .touchUpInside)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// This call is also ignored
UIScreen.main.brightness = CGFloat(0.8)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// This call sets the brightness
UIScreen.main.brightness = 0.5
}
@objc func toggle() {
brightness = brightness - 0.2
print("brightness is \(brightness)")
if brightness < 0 { brightness = 1.0 }
// This call changes the brightness
UIScreen.main.brightness = brightness
}
}
我发现了一些有趣的东西!
如果您从代码设置亮度 - 但随后用户从系统手动更改它 UI 您将无法再从代码更改亮度(直到应用程序重新启动) - Apple 可能有一些阻止的逻辑 frequent/unwanted 变化。
这只是我的观察——文档中没有此类信息
我刚开始制作一个新应用。代码 UIScreen.main.brightness = 1.0
在任何地方都不起作用。我在 viewDidLoad
中尝试过,在 viewWillAppear
中尝试过。我注释掉了其他所有内容,并使它成为唯一的 运行 代码行,但它不起作用。我无法改变亮度。我正在我的 iPhone X - iOS 13.4.1 真实设备上试用它。可能是什么原因?
编辑:还尝试了一个新的 Xcode 项目。还是不行。
我注意到在 iOS 13 中,从 viewDidLoad 和 viewWillAppear 调用 UIScreen.main.brightness 没有任何效果。将它添加到 viewDidAppear 工作正常。
它也可以很好地放入按钮操作。
import UIKit
class ViewController: UIViewController {
var brightness: CGFloat = 1.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let button = UIButton(type: .infoDark)
// This call is ignored
UIScreen.main.brightness = CGFloat(0.3)
view.addSubview(button)
button.frame = CGRect(x: 40.0, y: 100.0, width: button.frame.width, height: button.frame.height)
button.addTarget(self, action: #selector(toggle), for: .touchUpInside)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// This call is also ignored
UIScreen.main.brightness = CGFloat(0.8)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// This call sets the brightness
UIScreen.main.brightness = 0.5
}
@objc func toggle() {
brightness = brightness - 0.2
print("brightness is \(brightness)")
if brightness < 0 { brightness = 1.0 }
// This call changes the brightness
UIScreen.main.brightness = brightness
}
}
我发现了一些有趣的东西! 如果您从代码设置亮度 - 但随后用户从系统手动更改它 UI 您将无法再从代码更改亮度(直到应用程序重新启动) - Apple 可能有一些阻止的逻辑 frequent/unwanted 变化。
这只是我的观察——文档中没有此类信息