UIAlertController customization

2020-05-06 18:13发布

问题:

I'm trying to customize my UIAlertController with a dark theme.
I'm aiming for something like this

I'm trying different things, including the suggestion I found here https://stackoverflow.com/a/29122883/1817873, but for some reason only the first button gets colored while the cancel button keeps staying white.

Here is my code:

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?

回答1:

Any ideas?

Yes. Give up. An alert controller is very limited, and you should stick to what it does. But it is just a presented view controller, and nothing stops you from making your own presented view controller that looks and acts just like an alert, and since it is your view controller, it's your view and you can do whatever you like.



回答2:

If you're still struggling with this, I have a library that may be of help. It lets you create custom action sheets. It has a bunch of built-in types and can be extended and restyled as well.