0

Is there any way to customize the buttons of uialertcontroller? I have seen how to customize font, size and color but cannot add a background image or color of uialertcontroller button.

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                         message:nil
                                                                  preferredStyle:UIAlertControllerStyleAlert];
UIViewController *v = [[UIViewController alloc] init];
v.view.backgroundColor = [UIColor cyanColor];

[alertController setValue:v forKey:@"contentViewController"];

UIAlertAction *selectImage = [UIAlertAction
                                actionWithTitle:@"First Button"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction *action){

                                    NSLog(@"First Button");

                                }];

[alertController addAction:selectImage];

UIAlertAction *addImage = [UIAlertAction
                                 actionWithTitle:@"Second Button"
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction *action) {

                                     NSLog(@"Second Button");

                                 }];
[alertController addAction:addImage];

alertController.view.tintColor = [UIColor redColor];

UIView *subView = alertController.view.subviews.firstObject;
UIView *alertContentView = subView.subviews.firstObject;
[alertContentView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]]];

alertContentView.layer.cornerRadius = 5;

[self presentViewController:alertController animated:YES completion:nil];

Please suggest if there is any way to set background image of buttons.

Arnab
  • 4,216
  • 2
  • 28
  • 50

2 Answers2

0

You can add an accessory image:

UIAlertAction *selectImage = [UIAlertAction
                            actionWithTitle:@"First Button"
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction *action){

                                NSLog(@"First Button");

                            }];

[alertController addAction:selectImage];

UIImage *image = [UIImage imageNamed:@"Image.jpg"];
[selectImage setValue:image forKey:@"image"];

Also, as mentioned, you can create custom view and override touchesEnded.

Bretsko
  • 608
  • 2
  • 7
  • 20
  • Thanks for the effort. I have seen this earlier. Can you please explain what is `image` key here `[selectImage setValue:image forKey:@"image"]` – Arnab Jan 20 '16 at 13:21
  • @arnab-hore This key is not exposed through property, but you can find in runtime headers https://github.com/devSC/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIAlertAction.h – Bretsko Jan 20 '16 at 14:06
0

Try the below coding,it is helpful for you.

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:50.0]
              range:NSMakeRange(24, 11)];
[alertVC setValue:hogan forKey:@"attributedTitle"];



UIAlertAction *button = [UIAlertAction actionWithTitle:@"Label text"
                                                 style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction *action){
                                                   //add code to make something happen once tapped
                                               }];
UIImage *accessoryImage = [UIImage imageNamed:@"addusergroup.png"];
[button setValue:accessoryImage forKey:@"image"];
[self presentViewController:alertVC animated:YES completion:nil];
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • Thanks for the effort. I have tried this too. Can you please explain what is `image` key here `[button setValue:accessoryImage forKey:@"image"]` – Arnab Jan 20 '16 at 13:23