set alertview Yes button to bold and No button to normal

Fahim Parkar picture Fahim Parkar · Jan 27, 2014 · Viewed 13.4k times · Source

I have alertview where I have Yes and No options. It looks like below.

enter image description here

Code used is

UIAlertView *confAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
confAl.tag = 888;
[confAl show];

This is perfect but I want Yes to be bold and No as normal font.

So I switched the Yes and No button and have like below.

enter image description here

Code used is

UIAlertView *confAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
confAl.tag = 888;
[confAl show];

Is there any way where we can have Yes as first button and No as second button with Yes as bold effect?

Note : I want same effects in iOS 6 (same old style) & iOS 7 (new style as above in image) too.

Answer

Sk Borhan Uddin picture Sk Borhan Uddin · Jan 18, 2017

You may use preferredAction default property of UIAlertController instead of UIAlertView.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* yesButton = [UIAlertAction
                            actionWithTitle:@"OK"
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action)
                            {

                                [alertController dismissViewControllerAnimated:YES completion:nil];

                            }];

UIAlertAction* noButton = [UIAlertAction
                           actionWithTitle:@"Cancel"
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action)
                           {
                               [alertController dismissViewControllerAnimated:YES completion:nil];
                           }];

[alertController addAction:noButton];    
[alertController addAction:yesButton];
[alertController setPreferredAction:yesButton];

setPreferredAction will set your button title bold.