bikram sapkota
bikram sapkota

Reputation: 1124

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

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

Upvotes: 18

Views: 14979

Answers (8)

yoAlex5
yoAlex5

Reputation: 34311

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

Upvotes: 0

Ashutosh Mishra
Ashutosh Mishra

Reputation: 1939

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

Upvotes: 1

Sukh
Sukh

Reputation: 1398

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)

Upvotes: 2

Vinoth
Vinoth

Reputation: 9744

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.

Upvotes: 5

Aditya Jha
Aditya Jha

Reputation: 55

Just give the style property of the button as destructive.

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

})

Upvotes: 2

Igor
Igor

Reputation: 12303

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

Upvotes: 26

Nguyễn Anh Việt
Nguyễn Anh Việt

Reputation: 605

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

Upvotes: 6

kuldip bhalodiya
kuldip bhalodiya

Reputation: 1071

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....
                                      }];

Upvotes: 0

Related Questions