更改UIAlertController选中按钮的颜色

4

我有这些代码:

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

我想在取消按钮被选中时改变其颜色。我该怎么做? 请帮忙。

你有检查过这个链接吗?https://dev59.com/RmAf5IYBdhLWcg3w52I_ - jithin
@jithin 我查看了这个链接,但是当按钮被选中时,tintColor会更改为默认颜色。我的问题是关于选定按钮的颜色。 - Adela Toderici
3个回答

6

试一下

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

尝试在呈现警报控制器之后设置色调颜色:

尝试在呈现警报控制器之后设置色调颜色:

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

尝试在展示警告控制器后设置色调颜色:

在展示警告控制器后,尝试设置色调颜色:

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

朋友,祝你有美好的一天。同时,在@Ferrakkem Bhutan,Bhumica的回答也是正确的。 - Anbu.Karthik
如果我的答案正常工作,请为答案点赞,这对未来很有用。 - Anbu.Karthik
当我说“谢谢”时,我给了你点赞 :) @Anbu.Karthik - Adela Toderici

0

只需更改 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];

我也写了这行代码,但当按钮被选中时,色调颜色会变为默认颜色。 - Adela Toderici
这是iOS 9和Xcode 7.0.1中的一个bug,您可以在Xcode 7.1和iOS 9.1及以上版本中进行检查。 - Anbu.Karthik
它不起作用@Anbu.Karthik,我尝试了这个版本直到提问,我有Xcode 7.1和iOS 9.1。 - Adela Toderici

-1

试试这个

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

欲了解更多详情请点击此处。


这段代码是用于Swift而不是Objective-C的:)) @Ferrakkem Bhuiyan - Adela Toderici

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接