更改 UIAlertController 选定的按钮颜色

Change UIAlertController selected button color

我有这行代码:

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];

我想更改选中时取消按钮的颜色。我怎样才能做到这一点? 请帮忙。

只需更改 UIAlertController 视图的色调颜色即可。

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

试试这个

Swift

   {
        // Bugfix: iOS9 - Tint not fully Applied without Reapplying
        alertController.view.tintColor = UIColor.redColor()
    }

Objective-C

   {
        // Bugfix: iOS9 - Tint not fully Applied without Reapplying
        alertController.view.tintColor = [UIColor redColor];
    }

了解更多详情Click Here.

试试这个

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()