set alertview Yes button to bold and No button to normal
Asked Answered
J

2

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.

Jeroboam answered 27/1, 2014 at 11:17 Comment(4)
@downvoter : what is the problem in question? I seriously love to HATE you...Jeroboam
@Fahim.. Chill .. I've up voted you. don't hate that down voter ..:)Gosport
Could also try some of these : cocoacontrols.com/search?utf8=%E2%9C%93&q=alert+viewGatling
@JohnWoods : I don't want to go with custom alertview... I am trying to find solution with the current alertview... maybe like firstButton.title.style = bold or something like this...Jeroboam
G
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.

Gosport answered 27/1, 2014 at 11:26 Comment(4)
upvoted... can you take a look at my question hereJeroboam
@FahimParkar.. Can't see your screenshots.. will see question after an hour.. will intimate if got any ideaGosport
"In iOS 7 by default cancel button is the one which is highlighted"? Are you sure? It's exactly the opposite in the screenshots.Tortile
@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.Gosport
S
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.

Slusher answered 18/1, 2017 at 12:21 Comment(4)
If we're using UIAlertController, this is the answerEarwig
Since UIAlertView is being deprecated, this is the best answer going forwardIny
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?Ilailaire
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 that UIAlertController will be highlighting the last added action)Ilailaire
G
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.

Gosport answered 27/1, 2014 at 11:26 Comment(4)
upvoted... can you take a look at my question hereJeroboam
@FahimParkar.. Can't see your screenshots.. will see question after an hour.. will intimate if got any ideaGosport
"In iOS 7 by default cancel button is the one which is highlighted"? Are you sure? It's exactly the opposite in the screenshots.Tortile
@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.Gosport

© 2022 - 2024 — McMap. All rights reserved.