0

I have an Action Sheet with one Default action and one Cancel action. How can I change just Default action text color?

var refreshAlert = UIAlertController(title: "", message: "Are you sure you want to clear the entire message history? \n This cannot be undone.", preferredStyle: UIAlertControllerStyle.ActionSheet)

refreshAlert.addAction(UIAlertAction(title: "Clear message history", style: .Default, handler: { (action: UIAlertAction!) in
    print("Handle Ok logic here")
    // how to change this text color to red?
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
    print("Handle Cancel Logic here")
}))

2 Answers2

2

You cannot. Instead Change your method like below: Apple has provided red color for Destructive buttons.

refreshAlert.addAction(UIAlertAction(title: "Clear message history", style: .Destructive, handler: { (action: UIAlertAction!) in
    print("Voila !! button color is changed")
}))
iAnurag
  • 8,752
  • 3
  • 26
  • 45
0

I don't think there is a direct way to do that. However, there is bit of an ugly trick you can apply here:

  1. Create an image of exactly same size as that of UIAlertController action sheet style button.
  2. Make sure image looks like exactly same as that you want your UI to look like including text & colour.
  3. Pass that image as image property of your Default action like this:

->

let defaultAction = UIAlertAction(title: "Clear message history", style: .Default, handler: { (action: UIAlertAction!) in
        print("Handle Ok logic here")
        // how to change this text color to red?
})

let image = UIImage(named: "myButtton.png")
defaultAction.setValue(image, forKey: "image")
alertController.addAction(defaultAction)
Abhinav
  • 36,284
  • 39
  • 178
  • 301