Jacob Cavin
Jacob Cavin

Reputation: 2319

Change UIAlertController Background Tint

Yes, I know. You can't change the appearance of a UIAlertController. I don't want to change the background of the UIAlertController itself, I want to change the tint of the view's background. You know how it's like a transparent black color? I want to change that color to clear. How would I do this?

Upvotes: 7

Views: 1721

Answers (5)

Eugene Butkevich
Eugene Butkevich

Reputation: 91

You can create CustomAlertController, set your color in method viewWillAppear and show this controller instead of default UIAlertController. It's bad but working solution.

final class CustomAlertController: UIAlertController {
    
    //MARK: - Lifecycle

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        view.superview?.backgroundColor = yourColor
    }
}

Upvotes: -1

Digvijaysinh Gida
Digvijaysinh Gida

Reputation: 411

For swift 3 UIAlertController change background color.

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)

Upvotes: 4

Priyanka
Priyanka

Reputation: 452

Try this..

[alert.view setBackgroundColor:[UIColor whiteColor]];

Try changing whiteColor to whatever colour u wish. To fix the corner extra color, add corner radius and run. Hope this helps!

Upvotes: 0

Lawliet
Lawliet

Reputation: 3499

You can change the background colour as follows:

let alert = UIAlertController(title: "TITLE", message: "My message", preferredStyle: .alert)
if let subview = alert.view.subviews.first?.subviews.first?.subviews.first {
    subview.backgroundColor = .red
}
alert.view.tintColor = UIColor.black

Upvotes: 0

Fangming
Fangming

Reputation: 25261

No you cannot change the transparent background color. That does not belong to part of UIAlertController variable that you can control.

You will have to fix it by presenting a new view controller with a transparent background and a faked alert-controller-like UI element.

To further approve you does not have control for the alert controller, create another view with frame origin on 0,0 and then add to your alert controller's view. It will show on the top left corner of your pop up window, not the window of your window

Upvotes: 0

Related Questions