Adela Adela - 5 months ago 31
Swift Question

Change UIAlertController selected button color

I have this lines of code:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"cancel registration");
}];
[alertController addAction:cancelAction];
alertController.view.tintColor = [UIColor redColor];


I want to change the cancel button color when is selected. How can I do that?
Please help.

Answer

try this

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"cancel registration");
}];
[alertController addAction:cancelAction];

try setting the tint color AFTER you present your alert controller:

[self presentViewController: alertController animated:YES completion:nil];
 alertController.view.tintColor = [UIColor redColor];

Swift

var alertController: UIAlertController = UIAlertController.alertControllerWithTitle(title, message: nil, preferredStyle: .ActionSheet)
var cancelAction: UIAlertAction = UIAlertAction.actionWithTitle(cancelTitle, style: .Cancel, handler: {(action: UIAlertAction) -> Void in
    NSLog("cancel registration")
})
alertController.addAction(cancelAction)

try setting the tint color AFTER you present your alert controller:

self.presentViewController(alertController, animated: true, completion: { _ in })
alertController.view.tintColor = UIColor.redColor()
Comments