我想创建一个具有操作表样式的UIAlertController,但我想将背景颜色更改为灰色。我已经能够找到一种方法来改变UIAlertController的背景颜色,但不是取消按钮。取消按钮是单独的,保持白色。UIAlertController更改操作表的取消按钮的背景颜色
这是我现在所拥有的代码:
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];
这使我有以下结果: Link
我曾参观过How to change the background color of the UIAlertController? 但没有任何解决方案都为自定义颜色取消按钮的背景。
任何帮助,将不胜感激!
是否要更改取消按钮的背景颜色? – user3182143
查看我的编码我将取消按钮颜色更改为darkGray颜色。 – user3182143
我希望取消按钮具有深灰色背景色和白色文本。你的代码似乎没有这样做,但是相反:你的取消按钮的背景颜色是白色的,而你的文本颜色是灰色的 – Pibomb