2

Is it possible to change the background color of UIAlertController? I have managed to change the background color of UIAlertView. I have gone through third parties also to do the same, but I want o do it without use of any third party.

Below is my code:

UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
                                                                  message:@"Message"
                                                           preferredStyle:UIAlertControllerStyleAlert];
    UIView *firstSubview = alert.view.subviews.firstObject;
    UIView *alertContentView = firstSubview.subviews.firstObject;
    for (UIView *subSubView in alertContentView.subviews) {
        subSubView.backgroundColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
        //Close Action
    }];

    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
        //Close Action
    }];

    [cancelAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
    [deleteAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

    [alert addAction:cancelAction];
    [alert addAction:deleteAction];
    [self presentViewController:alert animated:YES completion:nil];

Thanks in advance!!

User511
  • 1,456
  • 10
  • 28
iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54
  • 2
    please refer this link it will be help https://stackoverflow.com/a/29122883/9137841 –  Jan 02 '18 at 13:18
  • Try to change button tint colour. – phani Jan 02 '18 at 13:18
  • @JRB That change the alert view BG only. – iPhone Programmatically Jan 02 '18 at 13:25
  • While you can do this by searching down the sub views of the alert controller and looking for the buttons you shouldn't do it. This is because 1) Apple say to use it as is and not to modify it's view hierarchy (which includes changing properties) 2) It may break in a future iOS update and give you no way to replicate it. If you really need an alert controller style popup that is custom then you should design and implement one yourself. – Upholder Of Truth Jan 02 '18 at 15:23

0 Answers0