如何仅更改操作 Sheet 默认操作颜色?

How to change Action Sheet Default action color only?

我有一项操作 Sheet,其中包含一项默认操作和一项取消操作。如何仅更改默认操作文本颜色?

var refreshAlert = UIAlertController(title: "", message: "Are you sure you want to clear the entire message history? \n This cannot be undone.", preferredStyle: UIAlertControllerStyle.ActionSheet)

refreshAlert.addAction(UIAlertAction(title: "Clear message history", style: .Default, handler: { (action: UIAlertAction!) in
    print("Handle Ok logic here")
    // how to change this text color to red?
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
    print("Handle Cancel Logic here")
}))

你不能。而是像下面这样更改您的方法:Apple 为破坏性按钮提供了红色。

refreshAlert.addAction(UIAlertAction(title: "Clear message history", style: .Destructive, handler: { (action: UIAlertAction!) in
    print("Voila !! button color is changed")
}))

我认为没有直接的方法可以做到这一点。但是,您可以在此处应用一些丑陋的技巧:

  1. 创建与 UIAlertController 操作 sheet 样式按钮完全相同大小的图像。
  2. 确保图片看起来与您希望的 UI 看起来完全一样,包括文字和颜色。
  3. 将该图像作为您的 Default 操作的 image 属性 传递,如下所示:

->

let defaultAction = UIAlertAction(title: "Clear message history", style: .Default, handler: { (action: UIAlertAction!) in
        print("Handle Ok logic here")
        // how to change this text color to red?
})

let image = UIImage(named: "myButtton.png")
defaultAction.setValue(image, forKey: "image")
alertController.addAction(defaultAction)