UIAlertController 自定义

UIAlertController customization

我正在尝试使用深色主题自定义 UIAlertController。
我的目标是这样

我正在尝试不同的方法,包括我在此处找到的建议 ,但出于某种原因,只有第一个按钮变色,而取消按钮保持白色。

这是我的代码:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:deleteAction];
[alert addAction:cancelAction];

UIView *firstView = alert.view.subviews.firstObject;
UIView *nextView = firstView.subviews.firstObject;
nextView.backgroundColor = [UIColor blackColor];

有什么想法吗?

Any ideas?

是的。放弃。警报控制器非常有限,您应该坚持它的功能。但它只是一个呈现的视图控制器,没有什么能阻止你制作你自己的呈现视图控制器,它看起来和行为就像一个警报,因为它是你的视图控制器,它是你的视图,你可以做任何你喜欢的事情。

如果您仍在为此苦苦挣扎,我 a library 可能会对您有所帮助。它允许您创建自定义操作表。它有一堆 built-in 类型,也可以扩展和重新设置样式。