UIAlertController change background color of Cancel button for action sheet

后端 未结 5 1127
死守一世寂寞
死守一世寂寞 2020-12-18 15:38

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

5条回答
  •  情歌与酒
    2020-12-18 16:09

    I got the solution now. I created sample project and worked out for your question and I got it.

    ViewController.h

    #import 
    @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
    

    See the output

提交回复
热议问题