Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertAction with different color button text

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)
like image 546
user1079052 Avatar asked Mar 14 '17 18:03

user1079052


2 Answers

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)
like image 102
user1079052 Avatar answered Dec 30 '22 06:12

user1079052


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
like image 37
fullmoon Avatar answered Dec 30 '22 08:12

fullmoon