Add Custom UIButton to UIAlertController

With the release of iOS 9, UIAlertView has been deprecated and should be replaced with UIAlertController.

UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

There are plenty of tutorials showing how this can be done. If you’re looking to add custom buttons to your UIAlertController, the code below will show you how to do just that.


- (void)showColorPickerActionSheet { 
    //Add line breaks to give enough space for the buttons.  Is there a better way to do this?
    NSString *title = @"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; 
    alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@" ", @"")]
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                                   handler:^(UIAlertAction * action) {
                                                       [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
                                                   }];
    
    UIButton *red = [[UIButton alloc] initWithFrame:CGRectMake(20,10,260,30)];
    [red setBackgroundColor:[UIColor redColor]];
    [red addTarget:self action:@selector(colorPickerBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    red.showsTouchWhenHighlighted = YES;
    red.layer.cornerRadius = 3;
    [alert.view addSubview:red];
    
    UIButton *blue = [[UIButton alloc] initWithFrame:CGRectMake(20,50,260,30)];
    [blue setBackgroundColor:[UIColor blueColor]];
    [blue addTarget:self action:@selector(colorPickerBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    blue.showsTouchWhenHighlighted = YES;
    blue.layer.cornerRadius = 3;
    [alert.view addSubview:blue];
    
    UIButton *green = [[UIButton alloc] initWithFrame:CGRectMake(20,90,260,30)];
    [green setBackgroundColor:[UIColor greenColor]];
    [green addTarget:self action:@selector(colorPickerBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    green.showsTouchWhenHighlighted = YES;
    green.layer.cornerRadius = 3;
    [alert.view addSubview:green];
    
    UIButton *yellow = [[UIButton alloc] initWithFrame:CGRectMake(20,130,260,30)];
    [yellow setBackgroundColor:[UIColor yellowColor]];
    [yellow addTarget:self action:@selector(colorPickerBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    yellow.showsTouchWhenHighlighted = YES;
    yellow.layer.cornerRadius = 3;
    [alert.view addSubview:yellow];
    
    UIButton *orange = [[UIButton alloc] initWithFrame:CGRectMake(20,170,260,30)];
    [orange setBackgroundColor:[UIColor orangeColor]];
    [orange addTarget:self action:@selector(colorPickerBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    orange.showsTouchWhenHighlighted = YES;
    orange.layer.cornerRadius = 3;
    [alert.view addSubview:orange];
    
    UIButton *gray = [[UIButton alloc] initWithFrame:CGRectMake(20,210,260,30)];
    [gray setBackgroundColor:[UIColor grayColor]];
    [gray addTarget:self action:@selector(colorPickerBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    gray.showsTouchWhenHighlighted = YES;
    gray.layer.cornerRadius = 3;
    [alert.view addSubview:gray];
    
    UIButton *black = [[UIButton alloc] initWithFrame:CGRectMake(20,250,260,30)];
    [black setBackgroundColor:[UIColor blackColor]];
    [black addTarget:self action:@selector(colorPickerBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    black.showsTouchWhenHighlighted = YES;
    black.layer.cornerRadius = 3;
    [alert.view addSubview:black];
    
    UIButton *white = [[UIButton alloc] initWithFrame:CGRectMake(20,290,260,30)];
    [white setBackgroundColor:[UIColor whiteColor]];
    [white addTarget:self action:@selector(colorPickerBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    white.showsTouchWhenHighlighted = YES;
    white.layer.cornerRadius = 3;
    [alert.view addSubview:white];
    
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

Result
UIButton

Leave a Reply

Your email address will not be published. Required fields are marked *

Please Do the Math      
 

*