UIAlertController更改操作表的“取消”button的背景颜色

我想创build一个UIAlertController的动作表风格,但我想要将背景颜色更改为灰色。 我已经能够find一种方法来更改UIAlertController的背景颜色,而不是取消button。 取消button,单独,保持白色。

这是我现在的代码:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alert addAction:[UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"Option 2" style:UIAlertActionStyleDefault handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"Option 3" style:UIAlertActionStyleDefault handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; UIView *firstSubview = alert.view.subviews.firstObject; UIView *alertContentView = firstSubview.subviews.firstObject; for (UIView *subSubView in alertContentView.subviews) { subSubView.backgroundColor = [UIColor darkGrayColor]; // Here you change background } alert.view.tintColor = [UIColor whiteColor]; [self.controller presentViewController:alert animated:YES completion:nil]; 

这给了我以下结果: 链接

我曾经访问过如何更改UIAlertController的背景颜色? 但没有一个解决scheme具有自定义颜色的取消button的背景。

任何帮助,将不胜感激!

如果你想单独的取消button(UIAlertActionStyleCancel),你不能改变取消button的背景颜色。 如果这是你的优先,那么你必须做出自己的自定义视图。 否则,您可以简单地添加标题为“取消”的默认操作。

[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]]

(但它不会给你一个单独的button)。

我已经debugging了视图层次结构,并发现这一点。 在这里输入图像说明

我现在得到了解决scheme。 我创build了示例项目,并解决了你的问题,我明白了。

ViewController.h

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end 

ViewController.m

 #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alert addAction:[UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"Option 2" style:UIAlertActionStyleDefault handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"Option 3" style:UIAlertActionStyleDefault handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; UIView *subView = alert.view.subviews.lastObject; //firstObject UIView *alertContentView = subView.subviews.lastObject; //firstObject [alertContentView setBackgroundColor:[UIColor darkGrayColor]]; alertContentView.layer.cornerRadius = 5; [self presentViewController:alert animated:YES completion:nil]; alert.view.tintColor = [UIColor darkGrayColor]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

看到输出

在这里输入图像说明