Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set alertview Yes button to bold and No button to normal

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.

like image 661
Fahim Parkar Avatar asked Jan 27 '14 11:01

Fahim Parkar


2 Answers

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.

like image 109
Sk Borhan Uddin Avatar answered Nov 13 '22 18:11

Sk Borhan Uddin


Your requirement is to "Highlight" Yes button.. In iOS 7 by default cancel button is the one which is highlighted. Unfortunately you can't simply change alertViewStyle here. But you do have a workaround.

Look at this answer.

like image 20
Prince Agrawal Avatar answered Nov 13 '22 18:11

Prince Agrawal