laser2302 laser2302 - 7 months ago 103
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
}

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

Answer

Try using UIAlertActionStyle.Destructive.

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