Highlight Top Button in UIAlertView - iphone

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;

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.

There is a customer alert view DTAlertView.
I hope it can help you.

Related

How to set identical appeareance to buttons for an UIAlertView?

By default, a UIAlertView with two buttons has different alpha for each button.
Is there a way to make them look identical with different text, without subclassing UIAlertView, or making a custom alert view?
Here is what i used:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
kAlertTitleOrderType message:kAlertMessageOrderType delegate:delegate
cancelButtonTitle:nil otherButtonTitles:#"Collection", #"Delivery", nil];
If you set the cancel button to nil when calling initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: then no cancel button will be added (this is the one that looks different). You can then add other buttons as you require.

UIAlertView with 3 buttons > spacing on third button

I am creating a simple alert view with 3 buttons, each button has equal importance, there is no cancel button or anything like that:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:#"msg" delegate:self cancelButtonTitle:nil otherButtonTitles:#"button1",#"button2",#"button3",nil];
[alert show];
The third button has extra space above it, as if it was a cancel button.
I would like the space to be identical in between buttons.
Any idea?
Unfortunately, there is no easy way to do so (you'd have to literally reinvent the wheel, subclassing UIAlertView not necessarily being a very viable option).
However, you might be interested by the BlockAlertsAnd-ActionSheets library, that would give you nice, block based, alert views that do offer a consistent spacing between buttons.

UIAlertView, how to place a lot of buttons inside it?

In UIAlertView, how can I place a lot of buttons inside it?
I've got an alert view and want to place ten buttons inside of it, but when in place, then they don't fit in the screen?
Moreover, how should I make the buttons look a little bit different, and how can I change their sizes?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Фильтр по категориям" message:#"Выбирите категорию:" delegate:self cancelButtonTitle:#"Отменить"
otherButtonTitles:#"Все", #"Избранное", #"Тосты", #"Рассказы", #"Стихи", #"Анекдоты", #"Афоризмы", #"Объявления", #"Разное", nil];
In English:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: # "Filter by Category" message: # "Choose a category:" delegate: self cancelButtonTitle: # "Cancel"
otherButtonTitles: # "All", # "Music", # "Toast", # "Stories", # "Poems", # "Jokes", # "Aphorisms", # "Announcements", # "Other", nil];
You should not place ten buttons inside a UIAlertView.
I don't know Russian so I have no clue as to the context of your problem. But there's no reason to put that many choices in an alert view.
Lorean is correct.
On the iPhone, for selecting a category filter, you should use a modal ViewController (with presentModalViewController), which will roll over your main view, allow them to select their category and then disappear.
On the iPad, you should use a UIPopoverController.
You might want to look on alternative alert view implementations https://github.com/TomSwift/TSAlertView
But be honest to yourself, if so many buttons make sense on a alert view. You also can use presentModalViewController to present a custom view, where you have all flexibility.

change position of cancel button in UIAlertView?

I noticed that when I delete an app from my iPhone home screen, the alert view that appears shows a Delete button on the left and Cancel on the right. However, when I build a delete function within my app using UIAlertView, the buttons only seem to display with Cancel on the left and Delete on the right.
I'd like my app to be consistent with the OS, but I can't figure out how to make the Cancel button appear first. Does anyone know?
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Delete Song"
message:#"Are you sure you want to delete this song? This will permanently remove it from your database."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Delete", nil];
I tried setting alert.cancelButtonIndex = 1, but that had no effect.
Ah, I just figured out how to change this. The cancelButtonTitle argument is optional, so you can add a custom button in whatever position you want and then designate that as the cancel button, like this:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Delete Song"
message:#"Are you sure you want to delete this song? This will permanently remove it from your database."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Delete", #"Cancel", nil];
alert.cancelButtonIndex = 1;
That puts the Delete button the left and the Cancel button on the right and highlights the Cancel button.
A possible reason Apple used an alert view on the home screen was because it once asked users to rate the apps they were removing (not anymore). They likely made the Cancel button the lighter-colored one because this was considered a destructive action (deletes an app and its data).
I guess you could reverse the titles (cancelButtonTitle:#"Delete" otherButtonTitles:#"Cancel", nil) and handle clicks on those buttons the other way around (not sure if Apple did the same). That would be a little awkward though; how about using an action sheet instead?

UIAlertView includes UITextField only display on top half screen

After several hours test, in iOS 4.0 and above I found if I draw an UITextField on an UIAlertView, sdk always make the UIAlertView only display on top half screen.
This case is only available on iOS 4.0 and above.
I guess maybe Apple worry about the prompted keyboard will hide the 'Cancel' or 'Ok' button of UIAlertView if the height of UIAlertView is too high.
Is it possible to disable the above function and make the UIAlertView display at center of screen?
I hope add more controls on UIAlertView and I think I can control the keyboard return button very well.
Welcome any comment
Thanks
interdev
I don't know about iOS4 but you can move it using:
UIAlertView *prompt = [[UIAlertView alloc] initWith......];
prompt.transform = CGAffineTransformMakeTranslation(0.0f, 50.0f);
[prompt show];