简体   繁体   中英

How do I show different color for cancel in iOS alert?

Is there a possible way to add a different color to cancel for iOS alert using Swift?

My code is as follows:

@objc func showAlert(){
    let codeNotReceivedAlert = UIAlertController(title: "Code not received?", message: "Resend security code (it can take up to a minute to arrive)", preferredStyle: UIAlertController.Style.alert)
    codeNotReceivedAlert.view.tintColor = UIColor(#colorLiteral(red: 0, green: 0.8465872407, blue: 0.7545004487, alpha: 1))
    codeNotReceivedAlert.addAction(UIAlertAction(title: "Resend", style: .default, handler: { (action: UIAlertAction!) in
        
    }))
    codeNotReceivedAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
        
    }))
    
    present(codeNotReceivedAlert, animated: true, completion: nil)
}

When I have run your code then I found an alert with the following details:-

在此处输入图像描述

1st Solution:-

If you want to change the cancel button color with red color(change cancel button style as destructive) then you should use the following code:-

 @objc func showAlert(){
    let codeNotReceivedAlert = UIAlertController(title: "Code not received?", message: "Resend security code (it can take up to a minute to arrive)", preferredStyle: UIAlertController.Style.alert)
    codeNotReceivedAlert.view.tintColor = UIColor(#colorLiteral(red: 0, green: 0.8465872407, blue: 0.7545004487, alpha: 1))
    codeNotReceivedAlert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: { (action: UIAlertAction!) in

    }))

    codeNotReceivedAlert.addAction(UIAlertAction(title: "Resend", style: .default, handler: { (action: UIAlertAction!) in

    }))

    present(codeNotReceivedAlert, animated: true, completion: nil)
}

Then the output should be like this:-

在此处输入图像描述

=================================================================

2nd Solution:-

And if you want to another/custom color for the cancel button and also don't want to change cancel button style then You can use the following code:-

  @objc func showAlert(){

    let alertController = UIAlertController(title: "Code not received?", message: "Resend security code (it can take up to a minute to arrive)", preferredStyle: .alert)
         alertController.view.tintColor = UIColor(#colorLiteral(red: 0, green: 0.8465872407, blue: 0.7545004487, alpha: 1))

           // Create Resend button
           let OKAction = UIAlertAction(title: "Resend", style: .default) { (action:UIAlertAction!) in

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

           }
           alertController.addAction(OKAction)

           // 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.yellow, forKey: "titleTextColor")

           alertController.addAction(cancelAction)

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

For example, I have used cancel button as yellow color the output like this:-

在此处输入图像描述

No.

Apple likely does not want to allow different things like this. They want to keep consistency between apps, so they often constrain UI elements' capabilities.

You can make your own alert that supports colors, but there is probably a reason Apple did not add it themselves.

You can't if you mean to employ UIAlertController to do this. UIAlertController can only be used with three kind of styles of UIAlertAction :

extension UIAlertAction {
    @available(iOS 8.0, *)
    public enum Style : Int {
        case `default` = 0
        case cancel = 1
        case destructive = 2
    }
}

Here is the code and screenshot to demonstrate:

func showAlert() {
    let alert = UIAlertController(
        title: "Do you mean to discard the changes?",
        message: "Going back may cause data lose",
        preferredStyle: .alert
    )
        
    let saveAndLeave = UIAlertAction(title: "Save before leaving", style: .default) { (_) in
        print("👋")
    }
    alert.addAction(saveAndLeave)
        
    let leaveDirectly = UIAlertAction(title: "Confirmed", style: .destructive) { (_) in
        print("😢")
    }
    alert.addAction(leaveDirectly)
        
    let cancel = UIAlertAction(title: "Let me think", style: .cancel) { (_) in
        print("😊")
    }
    alert.addAction(cancel)
        
    present(alert, animated: true, completion: nil)
}

截屏

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM