简体   繁体   English

如何在 iOS 警报中显示不同的取消颜色?

[英]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?是否有可能使用 Swift 为 iOS 警报添加不同的颜色以取消?

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:-那么 output 应该是这样的:-

在此处输入图像描述

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

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:-例如,我将取消按钮用作黄色 output,如下所示:-

在此处输入图像描述

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.他们希望保持应用程序之间的一致性,因此他们经常限制 UI 元素的功能。

You can make your own alert that supports colors, but there is probably a reason Apple did not add it themselves.您可以自己制作支持 colors 的警报,但苹果自己没有添加它可能是有原因的。

You can't if you mean to employ UIAlertController to do this.如果您打算使用UIAlertController来执行此操作,则不能。 UIAlertController can only be used with three kind of styles of UIAlertAction : UIAlertController只能与 UIAlertAction 的三种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)
}

截屏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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