簡體   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