UIAlertAction with different color button text

12,703

Solution 1

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)

Solution 2

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
Share:
12,703
user1079052
Author by

user1079052

Updated on June 21, 2022

Comments

  • user1079052
    user1079052 about 2 years

    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)
    
  • gEeKyMiNd
    gEeKyMiNd about 7 years
    I used it in objective C , it crashes the application. any suggestion for objective C ???
  • user1079052
    user1079052 over 6 years
    I haven't tried it out but here are some suggestions for objective c stackoverflow.com/questions/36662233/…
  • andrewlundy
    andrewlundy about 3 years
    This works when the programmer knows the button is going to represent an action that will change or delete data. It turns the action's title color to red, but it should not be used to explicitly change the color of a title. Unless of course, you're expecting it to be red. The best way to change the color of the title is @user1079052 answer. Though, it leaves the chance of Apple changing the string of the key in the future, so be aware of that.