繁体   English   中英

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

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

是否有可能使用 Swift 为 iOS 警报添加不同的颜色以取消?

我的代码如下:

@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)
}

当我运行您的代码时,我发现了一个包含以下详细信息的警报:-

在此处输入图像描述

第一种解决方案:-

如果您想用红色更改取消按钮颜色(将取消按钮样式更改为破坏性),那么您应该使用以下代码:-

 @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)
}

那么 output 应该是这样的:-

在此处输入图像描述

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

第二种解决方案:-

如果您想为取消按钮设置另一种/自定义颜色并且不想更改取消按钮样式,那么您可以使用以下代码:-

  @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)
}

例如,我将取消按钮用作黄色 output,如下所示:-

在此处输入图像描述

不。

苹果可能不希望允许这样的不同事情。 他们希望保持应用程序之间的一致性,因此他们经常限制 UI 元素的功能。

您可以自己制作支持 colors 的警报,但苹果自己没有添加它可能是有原因的。

如果您打算使用UIAlertController来执行此操作,则不能。 UIAlertController只能与 UIAlertAction 的三种UIAlertAction使用:

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

这是演示的代码和屏幕截图:

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