set alertview Yes button to bold and No button to normal

喜夏-厌秋 提交于 2019-12-22 03:50:31

问题


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

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.

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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21379449/set-alertview-yes-button-to-bold-and-no-button-to-normal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!