10

I am trying to create an UIAlertAction that has black color text for the first button and blue for the second. If I change the tint then all go black but i can't change the first button to black without it. Below is my code:

let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
// alert.view.tintColor = UIColor.black

alert.addAction(UIAlertAction(title: "First", style: .cancel, handler: nil))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

self.present(alert, animated: true, completion: nil)
Ashley Mills
  • 41,127
  • 14
  • 115
  • 144
user1079052
  • 3,395
  • 4
  • 24
  • 46
  • I don't think UIAlertController allows for different colors for different buttons except the style given i.e cancel, default etc – Rajan Maheshwari Mar 14 '17 at 18:10
  • 2
    The color is based on the style - .cancel is black, .default is black, and .destructive is red. There's no way to change the color unless you create your own custom UIAlertController. – creeperspeak Mar 14 '17 at 18:11
  • Not at all, UIAlertController buttons, like and some other PopUp controllers use colour from ```window.tint`` If you override this color then default colours change to your colour. – iTux Jul 31 '18 at 15:59

2 Answers2

36

I was able to do it by adding this or each color

let cancelAlert = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler:nil)
                cancelAlert.setValue(UIColor.blue, forKey: "titleTextColor")
                alert.addAction(cancelAlert)
user1079052
  • 3,395
  • 4
  • 24
  • 46
  • I used it in objective C , it crashes the application. any suggestion for objective C ??? – gEeKyMiNd Jun 07 '17 at 07:15
  • I haven't tried it out but here are some suggestions for objective c https://stackoverflow.com/questions/36662233/change-title-color-in-uialertcontroller – user1079052 Nov 08 '17 at 14:16
3

For those who are here to just change the color of an action button, you can change the style like for example .destructive for delete button:

 alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler:{(UIAlertAction) in
fullmoon
  • 6,463
  • 3
  • 34
  • 52