Home > Blockchain >  How to set a black background color to an alert?
How to set a black background color to an alert?

Time:05-25

I am currently developing an iOS application. I have customized my alerts to match the colors of my app. Here is the code:

func createPopUp(title: String, message: String, preferredStyle: UIAlertController.Style) -> UIAlertController {
    let alert = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
    
    alert.setTitlet(font: UIFont.boldSystemFont(ofSize: 17), color: UIColor.customOrange)
    alert.setMessage(font: UIFont.systemFont(ofSize: 13), color: UIColor.customOrange)
    
    let subview = alert.view.subviews.first! as UIView
    let alertContentView = subview.subviews.first! as UIView
    alertContentView.backgroundColor = UIColor.black // This is the important line
    alertContentView.tintColor = UIColor.customOrange
    alertContentView.layer.cornerRadius = 15;
    
    return alert
}

Only when I launch the app, I get this rendering:

RenderingBlack

The background color is not black. However when I change the UIColor, for example :

alertContentView.backgroundColor = UIColor.green

The rendering is correct:

RenderingGreen

In spite of research on the net I can't find the solution.

Thanks for your time :)

CodePudding user response:

Hello fiend please replace alertContentView.backgroundColor = UIColor.black with alert.view.subviews.first?.subviews.first?.subviews.first?.backgroundColor = .black

It will solve your problem.

  • Related