在UIAlertController中更改preferredStyle为.ActionSheet时取消按钮的颜色如何变化?

18

如何将取消按钮的颜色更改为红色,我知道可以使用“破坏性样式”来实现


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

但我希望取消按钮单独显示,像这样 在此输入图片描述


@Miteshjadav 这个问题是关于取消按钮的。tintColor 相反地会改变其他按钮的颜色。已报告。 - Vyachaslav Gerchicov
8个回答

26
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) 
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")

这个解决方案有多安全?如果苹果在未来的iOS更新中决定更改密钥名称怎么办? - Au Ris
我在应用程序中使用了这个解决方案,并成功地通过了iTunes的审核。 - Igor
如何在取消按钮中使用自定义字体? - Samiul Islam Sami

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

您需要使用两种不同的样式。 在这里,我使用了 .destructive.default,它们将把警告操作分成两部分。


2
.destructive不会像@bikram sapkota想要的那样将取消按钮分开,即使我们在其他操作中使用.default。 - Nishad Arora
@NishadArora 的评论是正确的。.destructive 不会使取消按钮分开。 - jpulikkottil

5

Swift 4

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

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

希望这能帮到您。

如何为“取消”按钮更改背景颜色? - Sai kumar Reddy
如何更改取消按钮的背景颜色? - Yogesh Patel

2
只需将按钮的样式属性设置为“destructive”。
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
            (alert: UIAlertAction!) -> Void in

})

2

Swift4.2

如果您有多个UIAlertAction,则可以像这样在UIAlertController中添加“取消”UIAlertAction

 let alert = UIAlertController(title: "Title", message: "Your Message", preferredStyle: UIAlertController.Style.actionSheet)
 alert.addAction(UIAlertAction(title: "first",style: .default, handler: { action in
     //Do something....           
   }))
 alert.addAction(UIAlertAction(title: "second", style: .default, handler: { action in
     //Do something....           
   }))
// Add cancel UIAlertAction
 let cancelAlert = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
     cancelAlert.setValue(UIColor.red, forKey: "titleTextColor")
     alert.addAction(cancelAction).  
     self.present(alert, animated: true, completion: nil)

如何更改取消按钮的背景颜色? - Yogesh Patel

1
如果您希望取消按钮的输出与不更改取消按钮类型为破坏性相同。我已在代码中使用取消类型来取消取消按钮。要实现相同的效果,您可以使用以下代码:
 //MARK:- Function to create the action sheet

  func showAlertSheet(){

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    // Create Google Map button
    let googleMap = UIAlertAction(title: "Open in Google Maps", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(googleMap)

    // Create Map button
    let map = UIAlertAction(title: "Open in Maps", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(map)

     // Create copy Address button
    let copyAddress = UIAlertAction(title: "Copy Address", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");

    }
    alertController.addAction(copyAddress)

    // 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.red, forKey: "titleTextColor")

    alertController.addAction(cancelAction)

    // Present Dialog message
    self.present(alertController, animated: true, completion:nil)
}

同时您还可以选择更改取消按钮的文本颜色。代码的输出如下所示:

enter image description here


0

你可以使用 alert.view.tintColor,它将应用于.cancel.default样式


0

将目标C中的样式从UIAlertActionStyleDefault更改为UIAlertActionStyleDestructive

UIAlertAction* button = [UIAlertAction actionWithTitle:@"Button title here"
                                      style:UIAlertActionStyleDestructive
                                      handler:^(UIAlertAction * action)
                                      {
                                          // Handle action here....
                                      }];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接