18

Is it possible to change the color of cancel button to red , i know we can by using Destructive style

  let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Destructive) { action -> Void in
            print("Cancel")
        }

but i want the cancel button separately , like this enter image description here

bikram sapkota
  • 1,106
  • 1
  • 13
  • 18

8 Answers8

26
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) 
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
Igor
  • 12,165
  • 4
  • 57
  • 73
6

This is code of how to make the alert like you said:

let alert = UIAlertController(title: "Hello", message: "Hello World", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Open in Google Maps", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Open in Google", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Copy Address", style: . default, handler: nil))

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

You have to use 2 kind of style. In here, I used .destructive and .default, It will separate alert action into 2 part

5

Swift 4

You can change the color of the alert action button using the below code.

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")

Hope this helps you.

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
2

Just give the style property of the button as destructive.

let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
            (alert: UIAlertAction!) -> Void in

})
Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
Aditya Jha
  • 55
  • 5
2

Swift4.2

If you have multiple UIAlertAction, then add "Cancel" UIAlertAction in UIAlertController like that.

 let alert = UIAlertController(title: "Title", message: "Your Message", preferredStyle: UIAlertController.Style.actionSheet)
 alert.addAction(UIAlertAction(title: "first",style: .default, handler: { action in
     //Do something....           
   }))
 alert.addAction(UIAlertAction(title: "second", style: .default, handler: { action in
     //Do something....           
   }))
// Add cancel UIAlertAction
 let cancelAlert = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
     cancelAlert.setValue(UIColor.red, forKey: "titleTextColor")
     alert.addAction(cancelAction).  
     self.present(alert, animated: true, completion: nil)
Sukh
  • 1,278
  • 11
  • 19
1

If you want to achieve the same output for the cancel button and also don't want to change the cancel button type to destructive. I have used cancel type for the cancel button in the code. To achieve the same, You can use the following code:-

 //MARK:- Function to create the action sheet

  func showAlertSheet(){

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    // Create Google Map button
    let googleMap = UIAlertAction(title: "Open in Google Maps", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(googleMap)

    // Create Map button
    let map = UIAlertAction(title: "Open in Maps", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(map)

     // Create copy Address button
    let copyAddress = UIAlertAction(title: "Copy Address", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(copyAddress)

    // Create Cancel button
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
        print("Cancel button tapped");
    }
    // Change Cancel title color according to your requirements
    cancelAction.setValue(UIColor.red, forKey: "titleTextColor")

    alertController.addAction(cancelAction)

    // Present Dialog message
    self.present(alertController, animated: true, completion:nil)
}

And also you have the option to change the cancel button text color. The output of the code is like this:-

enter image description here

Ashutosh Mishra
  • 1,679
  • 1
  • 13
  • 16
0

Change the style from UIAlertActionStyleDefault to UIAlertActionStyleDestructive in objective C:

UIAlertAction* button = [UIAlertAction actionWithTitle:@"Button title here"
                                      style:UIAlertActionStyleDestructive
                                      handler:^(UIAlertAction * action)
                                      {
                                          // Handle action here....
                                      }];
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
kuldip bhalodiya
  • 992
  • 7
  • 11
0

You can use alert.view.tintColor which will be applied for .cancel and .default styles

yoAlex5
  • 29,217
  • 8
  • 193
  • 205