Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIAlertController前景在不应显示时变暗_Ios_Iphone_Swift_Uialertcontroller - Fatal编程技术网

Ios UIAlertController前景在不应显示时变暗

Ios UIAlertController前景在不应显示时变暗,ios,iphone,swift,uialertcontroller,Ios,Iphone,Swift,Uialertcontroller,我有一个奇怪的问题,它影响了我整个应用程序中显示的所有UIAlertController——AlertController的白色前景显得暗淡,或者不如它应该的那样明亮 描述它的最简单方法是说明我通过将相关代码放入新的“测试”单视图应用程序(Swift 3-我应该注意,实际应用程序是使用Swift 2.3的单视图应用程序)中获得的预期效果 在我的“测试”应用程序中,下面的代码使用不推荐的AlertView显示示例消息,然后构建AlertController并在“之后”立即显示。除了突出显示前景“白

我有一个奇怪的问题,它影响了我整个应用程序中显示的所有UIAlertController——AlertController的白色前景显得暗淡,或者不如它应该的那样明亮

描述它的最简单方法是说明我通过将相关代码放入新的“测试”单视图应用程序(Swift 3-我应该注意,实际应用程序是使用Swift 2.3的单视图应用程序)中获得的预期效果

在我的“测试”应用程序中,下面的代码使用不推荐的AlertView显示示例消息,然后构建AlertController并在“之后”立即显示。除了突出显示前景“白色”中的对比度外,无需使用AlertView——如果我注释掉AlertView,问题行为也是一样的

问题行为:

图1:测试应用程序:显示AlertView,然后在其后面显示AlertController。注意AlertView前景的白度/亮度

图2:测试应用程序:当AlertView被关闭时,AlertController将成为最上面的viewcontroller,正如预期和所需,其前景亮度与AlertView之前的亮度完全相同

图3:real app:相同的代码,也位于ViewDidDisplay中-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()  

根据苹果公司的说法,“UIAlert​控制器类旨在按原样使用,不支持子类化。该类的视图层次结构是私有的,不能修改。“好吧,这是对它的黑客攻击,它证明了alertController在显示它之前获得了部分透明的背景色

通过将backgroundColor设置为UIColor.whiteColor(),问题已经解决,但解决方案应该是不必要的——问题究竟是如何首先出现的

当苹果公司甚至不向我展示背景色的时候,背景色是如何设置成透明的


感谢您的回答

看到你上传的图片后,我可以提一点

  • 如果您注意到,
    UIAlertController
    有点透明

  • 由于
    UIAlertController
    是透明的,因此背景会影响alert controller的外观

  • 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.
    }
    
注:案例1和案例2的背景为白色,案例3和案例4的背景为黑色

所以你们能做的就是使背景像案例1和案例2一样明亮(从图中)

**编辑:**

要更改
UIAlertController
的背景色,您可以执行以下操作

let subView = alertController.view.subviews.first!;
var alertContentView = subView.subviews.first!;
alertContentView.backgroundColor = UIColor.darkGray;
alertContentView.layer.cornerRadius = 5;

UIAlertView和UIAlertController具有相同的背景色和alpha值。
但在您的情况下,AlertView显示在AlertController上方,即为什么感觉UIAlertView比UIAlertController更白。

您错了,亮度是一样的,您只是有白色背景,用相同的条件向我们显示真实测试谢谢,但情况并非如此,在图像1、2和3中,AlertView、AlertController和AlertView,如图1和图3所示,分别为白色-在图1和图3中,AlertController位于AlertView后面,因此如图所示变暗。不期望的是,换句话说,问题出在图4中的AlertController,它应该与图1、2和3中的AlertView、AlertController和AlertView一样白,但事实并非如此。。它应该与图2中AlertController的白度相同,但不是。。。这就是问题所在。请看上面的答案,事实证明UIAlertController的alpha值确实小于1——苹果的文档中没有说明这是如何发生的,但这正是我需要了解的。在我随后的测试中,我继续观察到UIAlertController第一次出现的瞬间是完全白色的背景,即alpha为1,但在几分之一秒后,它变得有点透明,这就是问题所在,谢谢Hanks,当我第一次阅读您的回复时,我无法在UIAlertController中检测到任何透明度,但是我随后进行了一项测试,在其后面的主viewController上放置了一个鲜红色按钮,可以肯定您是正确的,UIAlertController中确实存在一些透明度!现在我所要做的就是找出原因并解决它。所以,我很难找出原因
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.
}