Change the color of cancel button in UIAlertController with preferredStyle: .ActionSheet
Asked Answered
G

8

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

Goles answered 1/9, 2016 at 9:47 Comment(1)
@Miteshjadav the question was about cancel button. tintColor vice versa changes other buttons color. ReportedDuchy
B
26
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) 
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
Beria answered 18/7, 2017 at 18:28 Comment(3)
How safe is this solution? What if apple decides to change the key name in future updates of iOS?Radnorshire
I used this solution in apps, successfully approved on iTunes.Beria
How can I use custom font in cancel button?Calabresi
W
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

Woman answered 24/3, 2017 at 7:10 Comment(2)
.destructive does not makes the cancel button separate as @bikram sapkota wants even if we use .default with rest actions.Counteract
@NishadArora above comment is correct. .destructive does not makes the cancel button separateLikable
U
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.

Unseemly answered 21/6, 2018 at 18:23 Comment(2)
then how to change the background colour for cancel buttonTanka
how to change the background colour of the cancel button?Patronizing
L
2

Just give the style property of the button as destructive.

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

})
Lamoureux answered 29/12, 2016 at 12:0 Comment(0)
H
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)
Hackler answered 1/11, 2019 at 8:14 Comment(1)
how to change the background colour of the cancel button?Patronizing
S
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

Stepfather answered 16/7, 2020 at 12:15 Comment(0)
A
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....
                                      }];
Adonai answered 24/3, 2017 at 5:47 Comment(0)
D
0

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

Deadman answered 29/11, 2021 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.