14

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.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

2 Answers2

30

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.

Hannele
  • 9,301
  • 6
  • 48
  • 68
Sk Borhan Uddin
  • 813
  • 1
  • 9
  • 19
  • 1
    If we're using UIAlertController, this is the answer – FlySoFast Feb 06 '17 at 09:51
  • 1
    Since UIAlertView is being [deprecated](https://developer.apple.com/reference/uikit/uialertview), this is the best answer going forward – Hannele Mar 01 '17 at 15:54
  • I'm also using `setPreferredAction` in my code. But this *crashes* in iOS8 as it was not available in iOS8. Do you have any solution which will work consistently on iOS 8 and above? – Mohnish Hirudkar Jul 31 '17 at 14:02
  • I found a solution for my above mentioned question. We should add device specific check for API `setPreferredAction` as it should be called only if the device is iOS 9 and above. Additionally, I observed that below iOS 9, UIAlertController internally highlights the last added action (button). (It is not mentioned in [Apple doc](https://developer.apple.com/documentation/uikit/uialertcontroller/1620094-addaction) that UIAlertController will be highlighting the last added action) – Mohnish Hirudkar Aug 01 '17 at 06:06
5

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.

Community
  • 1
  • 1
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • upvoted... can you take a look at my question [here](http://stackoverflow.com/questions/21364163/status-bar-becomes-gradient-after-closing-video-in-landscape-mode) – Fahim Parkar Jan 27 '14 at 11:37
  • @FahimParkar.. Can't see your screenshots.. will see question after an hour.. will intimate if got any idea – Prince Agrawal Jan 27 '14 at 11:44
  • "In iOS 7 by default cancel button is the one which is highlighted"? Are you sure? It's exactly the opposite in the screenshots. – Mecki Dec 12 '14 at 16:59
  • @Mecki.. Ya it is.. ScreenShot might seem confusing as its up to you, what you name your cancel button. By Default, right most button is cancel button. – Prince Agrawal Dec 16 '14 at 07:14