change UIAlertcontroller background Color

前端 未结 7 899
孤城傲影
孤城傲影 2021-02-02 15:09

Ok so I have this alert that I am using and I want the background of it to be black not grey like it is. I have managed to change the colour of the text for the title and the me

相关标签:
7条回答
  • 2021-02-02 15:20

    Swift 5
    Write just one line of code using UIAlertController extension.

    alertController.setBackgroundColor(color: UIColor.black)
    

    Full documentation: http://www.swiftdevcenter.com/change-font-text-color-and-background-color-of-uialertcontroller/

    extension UIAlertController {
    
        //Set background color of UIAlertController
        func setBackgroundColor(color: UIColor) {
            if let bgView = self.view.subviews.first, let groupView = bgView.subviews.first, let contentView = groupView.subviews.first {
                contentView.backgroundColor = color
            }
        }
    
        //Set title font and title color
        func setTitlet(font: UIFont?, color: UIColor?) {
            guard let title = self.title else { return }
            let attributeString = NSMutableAttributedString(string: title)//1
            if let titleFont = font {
                attributeString.addAttributes([NSAttributedString.Key.font : titleFont],//2
                                              range: NSMakeRange(0, title.utf8.count))
            }
    
            if let titleColor = color {
                attributeString.addAttributes([NSAttributedString.Key.foregroundColor : titleColor],//3
                                              range: NSMakeRange(0, title.utf8.count))
            }
            self.setValue(attributeString, forKey: "attributedTitle")//4
        }
    
        //Set message font and message color
        func setMessage(font: UIFont?, color: UIColor?) {
            guard let message = self.message else { return }
            let attributeString = NSMutableAttributedString(string: message)
            if let messageFont = font {
                attributeString.addAttributes([NSAttributedString.Key.font : messageFont],
                                              range: NSMakeRange(0, message.utf8.count))
            }
    
            if let messageColorColor = color {
                attributeString.addAttributes([NSAttributedString.Key.foregroundColor : messageColorColor],
                                              range: NSMakeRange(0, message.utf8.count))
            }
            self.setValue(attributeString, forKey: "attributedMessage")
        }
    
        //Set tint color of UIAlertController
        func setTint(color: UIColor) {
            self.view.tintColor = color
        }
    }
    
    0 讨论(0)
  • 2021-02-02 15:24

    try this

    Swift2 and below

    let subview :UIView = alert.view.subviews. first! as UIView
    let alertContentView = subview.subviews. first! as UIView
    alertContentView.backgroundColor = UIColor.blackColor()
    

    Objective -C

    UIView *subView = alertController.view.subviews.firstObject; //firstObject
    UIView *alertContentView = subView.subviews.firstObject; //firstObject
    [alertContentView setBackgroundColor:[UIColor darkGrayColor]];
    
    alertContentView.layer.cornerRadius = 5;
    

    updated answer swift 3 and above

       let alert = UIAlertController(title: "validate",message: "Check the process", preferredStyle: .alert)
        let dismissAction = UIAlertAction(title: "Dismiss", style: .destructive, handler: nil)
        alert.addAction(dismissAction)
        self.present(alert, animated: true, completion:  nil)
        // change the background color
        let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
        subview.layer.cornerRadius = 1
        subview.backgroundColor = UIColor(red: (195/255.0), green: (68/255.0), blue: (122/255.0), alpha: 1.0)
    

    output

    iPhone

    iPad

    0 讨论(0)
  • 2021-02-02 15:24

    In case someone wants to have an opaque white background color he can do this with this one liner:

    UIVisualEffectView.appearance(whenContainedInInstancesOf: [UIAlertController.classForCoder() as! UIAppearanceContainer.Type]).backgroundColor = UIColor.white
    

    Note however this will work properly only with white color as other colors will appear differently because of the default visual effect.

    0 讨论(0)
  • 2021-02-02 15:26

    Swift 4.1 :

    This is the best way works for me :

    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)
    }
    

    0 讨论(0)
  • 2021-02-02 15:30

    Due to a known bug (https://openradar.appspot.com/22209332), the accepted solution doesn't work on iOS 9.

    See my full answer here: https://stackoverflow.com/a/37737212/1781087

    0 讨论(0)
  • 2021-02-02 15:38

    let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView subview.layer.cornerRadius = 1 subview.backgroundColor = UIColor.white

    0 讨论(0)
提交回复
热议问题