【发布时间】:2017-03-29 07:56:27
【问题描述】:
我有一个奇怪的问题,它影响了整个应用程序中显示的所有 UIAlertControllers - AlertControllers 的白色前景显得暗淡或不那么亮,而不是应该的。
描述它的最简单方法是说明我通过将相关代码放在新的“测试”单视图应用程序中获得的期望和预期效果(Swift 3 - 我应该注意实际应用程序是使用单视图应用程序斯威夫特 2.3)。
在我的“测试”应用程序中,下面的代码使用已弃用的 AlertView 来显示示例消息,然后构建一个 AlertController 并在“之后”立即显示。除了突出显示前景“白色”中的对比度外,无需使用 AlertView - 如果我注释掉 AlertView,问题行为是相同的。
图 1:测试应用程序:显示 AlertView,然后在其后面显示 AlertController。注意 AlertView 前景的白度/亮度。
图 2:测试应用程序:当 AlertView 被关闭时,AlertController 是最上面的视图控制器,并且正如预期和期望的那样,它的前景亮度与 AlertView 之前的完全一样
图 3:真实应用:相同的代码,同样位于 viewDidAppear - AlertView 以预期的白度/亮度显示
图 4:真实应用:当 AlertView 被关闭时,AlertController 位于最上方,但其前景远不如 AlertView 的明亮/白色,或者与测试应用示例中的图像 2 一样。
如前所述,即使我消除了 AlertView 步骤(仅用于对比),行为也是相同的。
进一步观察 - 通常当我观察行为时,只是在受影响的 AlertController 第一次出现时的瞬间,例如可能 1/4 或 1/8 秒,它的前景是正确的白度/亮度,但随后出现不久之后,根据所述问题,它会变暗。
另一个观察结果 - 实际应用中的一些 AlertController 以正确的亮度水平正确显示,但许多其他的却没有。
最后一个观察结果 - 我在 Xcode 中使用了“查看 UI 层次结构”,但受影响的 AlertController 上似乎没有任何东西。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
UIAlertView(title: "My AlertView Title", message: "My AlertView Message", delegate: self, cancelButtonTitle: "OK").show()
let alertTitle = "My AlertController Title"
let msg = "\r\r\r\r\r\r\r\rMy AlertController Msg"
let alertController = UIAlertController(title: alertTitle, message: msg, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Don't show this again", style: .cancel) { (action:UIAlertAction!) in
print("Dismiss button pressed")
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "Action 1", style: .default) { (action:UIAlertAction!) in
print("I'm action1!!")
}
alertController.addAction(OKAction)
let OK2Action = UIAlertAction(title: "Action2", style: .default) { (action:UIAlertAction!) in
print("I'm action2")
}
alertController.addAction(OK2Action)
present(alertController, animated: false, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
感谢您对这个谜题的任何帮助,谢谢!
编辑:
感谢@Richard G 让我走上正轨——我仍然有问题,但更清楚了。
所以,如果你并排看下面的两个屏幕截图,它们都来自真实的应用程序,都使用上面显示的代码来生成 UIAlertController(我现在删除了 UIAlertView 行,因为它不需要) , 左边和右边的唯一区别是右边添加了这3行代码:
let backView = alertController.view.subviews.last?.subviews.last
backView?.layer.cornerRadius = 10.0
backView?.backgroundColor = UIColor.whiteColor()
side by side UIAlertControllers 1 transparent 1 not
根据 Apple 的说法,“UIAlertController 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。”好的,所以这是对它的入侵,它证明了 alertController 在显示它之前以某种方式获得了一个部分透明的背景颜色。
通过将 backgroundColor 设置为 UIColor.whiteColor() 问题得到解决,但解决方案应该是不必要的 - 问题最初是如何出现的?
当 Apple 甚至不向我公开该属性以使其能够设置时,backgroundColor 是如何以某种方式设置为具有透明度的?
感谢收到任何答案!
【问题讨论】:
-
你错了,亮度是一样的,你只是有白色背景,给我们看看相同条件下的真实测试
标签: ios iphone swift uialertcontroller