UIAlertController 不把后台所有元素都变成B/W

UIAlertController does not turn all elements in the background to B/W

编辑:包含我的解决方案的存储库:UIAlertControllerDimmed


显示 UIAlertController 后,大部分背景 'dimmed' 变成黑色和白色。有些元素变暗,但不会变 B/W.

这些元素是(在屏幕截图中从上到下):

我找不到与此主题相关的任何内容。我还需要更改什么才能使这些项目变暗?

这是没有 UIAlertController 的:

]

你可以有一个辅助函数来动画颜色变化

fileprivate func dimElements(highlight: Bool) {
    UIView.animate(withDuration: 0.3) {
        self.sendButton.backgroundColor = highlight ? .red : .gray
    }
}

然后在 presenting/dismissing 警报时调用它。

let alert = UIAlertController(title: "Error", message: "Oops!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .cancel, handler: {_ in self.dimElements(highlight: true) })
alert.addAction(okAction)
self.dimElements(highlight: false)
present(alert, animated: true, completion: nil)

我认为这里发生的事情是您正在设置某些元素的 tintColor,并且 tintColor 的行为与 backgroundColortextColor(或图像中的颜色)。

When an alert or action sheet appears, iOS 7 automatically dims the tint color of the views behind it. To respond to this color change, a custom view subclass that uses tintColor in its rendering should override tintColorDidChange to refresh the rendering when appropriate.

例如,我创建了一个显示警报控制器的简单应用程序。我将左键色调颜色设置为清除颜色,将文本颜色设置为蓝色:

我将右键色调设置为系统绿色:

当我 运行 应用程序并显示警报控制器时,它看起来像这样

之前:

之后:

为了获得您正在寻找的行为,您需要遵循@Alexander 的回答中的建议。您需要在屏幕上创建四个图像的灰度版本,并为它们的过渡设置动画。

感谢您的帮助。

为了有一个更灵活的解决方案,我决定创建一个 UIAlertController 的子类,它捕获屏幕截图,将其转换为灰度颜色并在显示时将其插入 UIAlertController 后面。这样它就可以工作而无需做任何额外的工作,并且您不需要为默认情况下不会变为灰度颜色的每个元素实现淡入淡出动画。

Github repo: UIAlertControllerDimmed