Highlight Top Button in UIAlertView

橙三吉。 提交于 2019-12-28 16:02:50

问题


I've got a UIAlertView with 3 buttons displayed vertically by default in the UIAlertView. I'd like the top button to be bold/highlighted. From my understanding and testing, the 'cancel' button is the one that is highlighted. The problem is no matter how I set the cancel button, it is placed last in this row. I cannot get it to be the first button.

I've tried setting the cancel button explicitly

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                message:message
                                               delegate:self
                                      cancelButtonTitle:@"Top Button"
                                      otherButtonTitles:@"Middle Button", @"Bottom Button", nil];

as well as setting the index of the cancel button

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                       message:message
                                                      delegate:self
                                             cancelButtonTitle:nil
                                             otherButtonTitles:@"Top Button", @"Middle Button", @"Bottom Button", nil];
alert.cancelButtonIndex = 0;

回答1:


This problem is actually caused by changes Apple made in iOS 7. Prior to iOS 7 we were able to access the subviews of an UIAlertView by calling [alertView subviews]. But since iOS 7 doesn't give us access to any subviews ([alertView subviews].count will always return zero) we can't customize UIAlertViews the way we used to.

So the only way to achive your goal under iOS 7 is to build a custom view that looks like UIAlertView and then customize it as you like.

But if you're coding for an iOS version prior to iOS 7 than you could use this easy hack to access a button:

UIAlertView *alertView = [[UIAlertView alloc] init];
[alertView addButtonWithTitle:@"Yes"];
UIButton *yesButton = [alertView.subviews lastObject]; //is nil under iOS 7

This way you would get access to the first button. After that you can customize your UIAlertView as usual.

By the way: Apple did not only want to give all UIAlertViews the same design by changing the way we can customize them. The reason lies in HCI researches (Human-Computer-Interaction). People tend to think the bottom button is always the 'default' answer if that is the way it is implemented throughout all apps.
Also the bottom button is the only highlighted button in a UIAlertView. So its visual weight is stronger than the visual weight of the button with about the same amount of text. That's another factor why people tend to choose this one. And that is also the reason why the highlighted button never should cause disastrous and irreversible actions ('You wanna delete all your saved games' should always highlight the button 'Keep my saved games' and not the one telling 'Delete everything').
Therefore Apple always makes the Cancel Button the bottom one no matter in which order you added the buttons. So if your app doesn't make use of a fully custom interface and uses many User Interface Elements provided by Apple than I highly recommend you to not try to change that behavior and make the bottom button your 'default' button.




回答2:


There is a customer alert view DTAlertView.

I hope it can help you.



来源:https://stackoverflow.com/questions/19125249/highlight-top-button-in-uialertview

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