Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight Top Button in UIAlertView

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;
like image 579
Kevin_TA Avatar asked Oct 01 '13 20:10

Kevin_TA


1 Answers

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.

like image 152
Jörg Kirchhof Avatar answered Oct 05 '22 20:10

Jörg Kirchhof