SUDHAKAR RAYAPUDI SUDHAKAR RAYAPUDI - 6 months ago 24
iOS Question

How to Change the UIAlertAction button Color in iOS?

I've a UIAlertController and I've a bunch of UIAlertAcion Buttons. Now I need to show one button with other Color rather than the same color.

For Ex


Button1

Button2

Button3

Button1 and button3 should be in blue

and

button2 should be in red.


Is it possible ? How?

Just Throw your thoughts
...

My Code:

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Food Menu" message:@"Select the MenuItem" preferredStyle:UIAlertControllerStyleActionSheet];
for(int i= 0; i<[menus count];i++){

UIAlertAction *action = [UIAlertAction actionWithTitle:[menu objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
[actionSheet dismissViewControllerAnimated:YES completion:nil];
//do SomeWork

}];

if(i==currentIndex){
//Put button Color Red
}
else{
//put button color Blue
}
[actionSheet addAction:action];
}

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
[actionSheet dismissViewControllerAnimated:YES completion:nil];
}];

[actionSheet addAction:cancel];


[self presentViewController:actionSheet animated:YES completion:nil];
}

Answer

do like change the alert style:UIAlertActionStyleDestructive

UIAlertController *alertController = [UIAlertController
                          alertControllerWithTitle:alertTitle
                          message:alertMessage
                          preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction 
        actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
                  style:UIAlertActionStyleCancel
                handler:^(UIAlertAction *action)
                {
                  NSLog(@"Cancel action");
                }];

 UIAlertAction *resetAction = [UIAlertAction
         actionWithTitle:NSLocalizedString(@"Reset", @"Reset action")
                   style:UIAlertActionStyleDestructive 
                 handler:^(UIAlertAction *action)
                 {
                   NSLog(@"Reset action");
                 }];

UIAlertAction *okAction = [UIAlertAction 
        actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                  style:UIAlertActionStyleDefault
                handler:^(UIAlertAction *action)
                {
                  NSLog(@"OK action");
                }];

[alertController addAction:cancelAction];
[alertController addAction:resetAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
Comments