laser2302 laser2302 - 2 years ago 223
Swift Question

Change the color of one or specific buttons on UIAlertController in Swift

I have an action sheet type of UIController which shows two items, the first one is "Logout from App" and the other one is "Cancel". I want to change the font color of the button which shows "Logout from App" to red and leave the "Cancel" button with default color. How can I do that? Below is my code:

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

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

Answer Source

Try using UIAlertActionStyle.Destructive.

let logoutActionSheetButton=UIAlertAction(title: "Logout from App", style: UIAlertActionStyle.Destructive) { ... }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download