Search code examples
iosobjective-cuialertcontroller

UIAlertController change background color of Cancel button for action sheet


I am trying to create a UIAlertController with the action sheet style but I want to change the background color to a gray color. I have been able to find a way to change the background color of the UIAlertController, but not the Cancel button. The Cancel button, which is separate, remains white.

This is the code that I have right now:

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

And this gives me the following result: Link

I have visited How to change the background color of the UIAlertController? but none of the solutions have a custom color for the background of the Cancel button.

Any help would be appreciated!


Solution

  • You cannot change color of a default cancel button style. You need to create a custom view controller for the cancel button and set it as a content view controller of a cancel alert action. This way keeps the cancel button separately

    enter image description here

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    
    alertController.addAction(UIAlertAction(title: "Option 1", style: .default, handler: nil))
    alertController.addAction(UIAlertAction(title: "Option 2", style: .default, handler: nil))
    alertController.addAction(UIAlertAction(title: "Option 3", style: .default, handler: nil))
    
    alertController.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: nil))
    
    if let firstSubview = alertController.view.subviews.first, let alertContentView = firstSubview.subviews.first {
        for view in alertContentView.subviews {
            view.backgroundColor = .darkGray
        }
    }
    
    alertController.view.tintColor = .white
    
    let cancelButtonViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CancelButtonViewController")
    let cancelAction = UIAlertAction(title: "", style: .cancel, handler: nil)
    cancelAction.setValue(cancelButtonViewController, forKey: "contentViewController")
    
    alertController.addAction(cancelAction)
    
    present(alertController, animated: true, completion: nil)
    

    enter image description here