0

My situation is that I want to change the background color of the alerts as well as the text. I've seen some examples, but they don't appear to be working anymore. Here is what I've tried:

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: btnTitle, style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
        let subview :UIView = alert.view.subviews.last! as UIView
        let alertContentView = subview.subviews.last! as UIView
        alertContentView.backgroundColor = UIColor.black

I have also tried to do this:

alert.view.backgroundColor = UIColor.black

and also putting them in the completion handlers, but it doesn't appear to be working like previous versions. What is appearing instead is the alert with the usual rounded corners and outside the rounded corners, the black background appears in the back to make the alert a square box (normally its rounded and the part where the black is showing would be transparent). The color just won't go over the alert's white color.

bearsworth
  • 77
  • 1
  • 2
  • 11
  • Did you looked at this answer http://stackoverflow.com/questions/26863460/how-to-change-the-background-color-of-the-uialertcontroller ? – Roman Podymov Nov 07 '16 at 08:03
  • Yes, if you look at my code, I also did the same thing but it isn't working. I tried changing .subviews.first and subviews.last but no changes. There was another person who mentioned this might be an issue with tint and you had to use the view's tint color, but can't seem to get it working in the most updated version of xcode. – bearsworth Nov 07 '16 at 17:23

1 Answers1

0

you have to change the color(alert.view.backgroundColor = UIColor.green) before you do self.present(alert, animated: true, completion: nil) It Works for me like that.. i have used green and red

freek
  • 1
  • I have tried that as well. Still does not work have you tried in current version of xcode? The issue I'm having is that there is some tint color to it. Like if I wanted a deep green, it would be a light tint of that green, or if I used a black, it would be a tint of that black... Let me know if that is happening for you. – bearsworth Nov 07 '16 at 21:36
  • i just did like 1 minute ago. you even helped me with the line alert.view.backgroundColor = UIColor.red because i didn't knew how to change the color – freek Nov 07 '16 at 21:38
  • Your alert though, set the color to UIColor.clear or set it to UIColor.black and it will not be completely clear or black. I'm wondering how to get the whole color changed. Someone said it was a tint issue, but that is not working any longer. – bearsworth Nov 07 '16 at 21:42
  • Thanks for the help. The code you submitted actually does change it to darker. If I wanted black, it does work better for that. Okay now for what I really want :). What I'm actually looking for is this specific red color. I can't seem to get it playing with the blur effect unfortunately. I saw other uses like light / extra light to no avail. If you have a better idea, I'm trying to change the background to this color (hex) : #B71C1C – bearsworth Nov 07 '16 at 22:22
  • Also for for you and anyone... you can use this as a way to replace UIColor.color with a hexcolor: https://ghostbin.com/paste/vh6at – bearsworth Nov 07 '16 at 22:27
  • I just decided that I will just create a custom alert or some type of pop over... No point wasting time. Thanks for the help though :). – bearsworth Nov 13 '16 at 06:52