ios - 更改 UIAlertcontroller 背景颜色

标签 ios objective-c swift uialertcontroller

好的,所以我有这个正在使用的警报,我希望它的背景是黑色而不是灰色。我设法更改了标题和消息的文本颜色,但没有更改背景颜色。好到我想要的颜色。我已将其更改为绿色蓝色和白色,但不是黑色。当我尝试将其更改为黑色时,它会变成灰色。任何建议都会有所帮助,我们将不胜感激。我在这里试过了How to change the background color of the UIAlertController?这就是我到达现在位置的方式。

这是我现在要做的:

func showAlert(title:String, message:String) {

    //Set up for the title color
    let attributedString = NSAttributedString(string: title, attributes: [
        NSFontAttributeName : UIFont.systemFontOfSize(15), //your font here,
        NSForegroundColorAttributeName : UIColor.whiteColor()
        ])
    //Set up for the Message Color
    let attributedString2 = NSAttributedString(string: message, attributes: [
        NSFontAttributeName : UIFont.systemFontOfSize(15), //your font here,
        NSForegroundColorAttributeName : UIColor.whiteColor()
        ])

    let alert = UIAlertController(title: title,message: message, preferredStyle: .Alert)

    alert.setValue(attributedString, forKey: "attributedTitle")
    alert.setValue(attributedString2, forKey: "attributedMessage")
    //alert.view.tintColor = UIColor.whiteColor()
    let dismissAction = UIAlertAction(title: "Dismiss", style: .Destructive, handler: nil)
        alert.addAction(dismissAction)
    self.presentViewController(alert, animated: true, completion: nil)
    //set the color of the Alert
    let subview = alert.view.subviews.first! as UIView
    let alertContentView = subview.subviews.first! as UIView
    alertContentView.backgroundColor = UIColor.blackColor()
    //alertContentView.backgroundColor = UIColor.greenColor()
    //Changes is to a grey color :( 
    /*
    alertContentView.backgroundColor = UIColor(
        red: 0,
        green: 0,
        blue: 0,
        alpha: 1.0)
    //Also another Grey Color Not batman black
    */

    //alertContentView.backgroundColor = UIColor.blueColor()
    //turns into a purple



}

最佳答案

swift 4.1:

这是最适合我的方式:

func testAlert(){
    let alert = UIAlertController(title: "Let's See ..",message: "It Works!", preferredStyle: .alert)
    let dismissAction = UIAlertAction(title: "Dismiss", style: .default, handler: nil)

    // Accessing alert view backgroundColor :
    alert.view.subviews.first?.subviews.first?.subviews.first?.backgroundColor = UIColor.green

    // Accessing buttons tintcolor :
    alert.view.tintColor = UIColor.white

    alert.addAction(dismissAction)
    present(alert, animated: true, completion:  nil)
}

Test Image

关于ios - 更改 UIAlertcontroller 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293656/

相关文章:

通过套接字的 iOS 连接不起作用

ios - 如何快速从字典中获取所有索引值?

ios - 条形按钮项目未正确对齐

ios - AVAudioEngine 分离 channel

php - 如何查看从 Objective C cocoa 发送到 php 的 GET 请求的响应

ios - AppConnect:错误:AppConnect无法启动,因为[UIApplication sharedApplication]不是AppConnectUIApplication的实例

swift - Alamofire 4 错误请求 'extra argument in call'

ios - 在哪里可以找到 iPhone 设备的 UDID?如果 iTunes 预计会在 macOS 10.15 之前退役,预计会在 wwdc19 中公布

android - iOS 版 GCM 的优势?

Swift 二分查找失败到最后一个返回语句