let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
这是实现你所说的提示框的代码:
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
,它们将把警告操作分成两部分。
Swift 4
使用下面的代码可以改变警告操作按钮的color
。
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
(alert: UIAlertAction!) -> Void in
})
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)
//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)
}
同时您还可以选择更改取消按钮的文本颜色。代码的输出如下所示:
你可以使用 alert.view.tintColor
,它将应用于.cancel
和.default
样式
将目标C中的样式从UIAlertActionStyleDefault
更改为UIAlertActionStyleDestructive
:
UIAlertAction* button = [UIAlertAction actionWithTitle:@"Button title here"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
// Handle action here....
}];
tintColor
相反地会改变其他按钮的颜色。已报告。 - Vyachaslav Gerchicov