在Swift中更改UIAlertController上的一个或特定按钮的颜色

时间:2016-04-29 15:53:40

标签: ios swift uialertcontroller

我有一个UIController的操作表类型,显示两个项目,第一个是"从App"另一个是"取消"。我想更改按钮的字体颜色,显示"从App"红色并离开"取消"默认颜色的按钮。我怎样才能做到这一点?以下是我的代码:

let cancelActionSheetButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { 
    (cancelSelected) -> Void in
    print("Cancel Selected")
}

let logoutActionSheet = UIAlertController(title:"Logout", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

let logoutActionSheetButton = UIAlertAction(title: "Logout from App", style: UIAlertActionStyle.Default) { 
    (logoutSelected) -> Void in
}

logoutActionSheet.addAction(logoutActionSheetButton)
logoutActionSheet.addAction(cancelActionSheetButton)

self.presentViewController(logoutActionSheet, animated: true, completion:nil)

1 个答案:

答案 0 :(得分:5)

尝试使用UIAlertActionStyle.Destructive

let logoutActionSheetButton=UIAlertAction(title: "Logout from App", style: UIAlertActionStyle.Destructive) { ... }