Change the color of cancel button in UIAlertController with preferredStyle: .ActionSheet
是否可以将"取消"按钮的颜色更改为红色,我知道我们可以使用破坏性样式
1 2 3 | let cancelActionButton: UIAlertAction = UIAlertAction(title:"Cancel", style: .Destructive) { action -> Void in print("Cancel") } |
但我想要单独的取消按钮,例如
1 2 | let cancelAction = UIAlertAction(title:"Cancel", style: .cancel) cancelAction.setValue(UIColor.red, forKey:"titleTextColor") |
斯威夫特4
您可以使用以下代码更改警报操作按钮的
1 2 | let cancelAction = UIAlertAction(title:"Cancel", style: .cancel, handler: nil) cancelAction.setValue(UIColor.red, forKey:"titleTextColor") |
希望对您有帮助。
这是如您所说的如何发出警报的代码:
1 2 3 4 5 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)) |
您必须使用2种样式。
在这里,我使用了
迅捷4.2
如果您有多个
1 2 3 4 5 6 7 8 9 10 11 12 | 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) |
只需将按钮的style属性设置为破坏性的即可。
1 2 3 4 | let cancelAction = UIAlertAction(title:"Cancel", style: .destructive, handler: { (alert: UIAlertAction!) -> Void in }) |
如果要为取消按钮实现相同的输出,并且也不想将取消按钮的类型更改为破坏性的。我在代码中使用了取消类型作为"取消"按钮。要实现相同的目的,可以使用以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | //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) } |
此外,您还可以选择更改取消按钮的文字颜色。代码的输出是这样的:
在目标C中将样式从
1 2 3 4 5 6 | UIAlertAction* button = [UIAlertAction actionWithTitle:@"Button title here" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { // Handle action here.... }]; |