使用preferredStyle:.ActionSheet更改UIAlertController中取消按钮的颜色

bik*_*ota 13 uiactionsheet swift uialertcontroller

是否可以将取消按钮的颜色更改为红色,我知道我们可以使用破坏性样式

  let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Destructive) { action -> Void in
            print("Cancel")
        }
Run Code Online (Sandbox Code Playgroud)

但我想分开取消按钮,就像这样 在此输入图像描述

Igo*_*gor 18

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) 
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
Run Code Online (Sandbox Code Playgroud)


Ngu*_*iệt 6

这是如何像你说的那样发出警报的代码:

let alert = UIAlertController(title: "Hello", message: "Hello World", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Open in Google Maps", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Open in Google", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Copy Address", style: . default, handler: nil))

alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
Run Code Online (Sandbox Code Playgroud)

你必须使用2种风格.在这里,我用.destructive.default,它将警报动作分成2部分

  • 即使我们将 .default 与其余操作一起使用,.destroy 也不会像 @bikram sapkota 想要的那样使取消按钮分离。 (2认同)

Vin*_*ino 5

斯威夫特 4

你可以改变color的的警报操作使用下面的代码按钮。

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
Run Code Online (Sandbox Code Playgroud)

希望这对你有帮助。


小智 2

只需将按钮的样式属性设置为破坏性即可。

let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
            (alert: UIAlertAction!) -> Void in

})
Run Code Online (Sandbox Code Playgroud)