突出显示UIAlertView中的顶部按钮 [英] Highlight Top Button in UIAlertView
问题描述
我有一个UIAlertView,UIAlertView默认垂直显示3个按钮。我希望顶部按钮是粗体/突出显示。根据我的理解和测试,取消按钮是突出显示的按钮。问题是无论我如何设置取消按钮,它都放在此行的最后。我不能把它作为第一个按钮。
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;
推荐答案
此问题实际上是由Apple在iOS中所做的更改引起的7.在iOS 7之前,我们可以通过调用 [alertView子视图]
来访问 UIAlertView
的子视图。但由于iOS 7不允许我们访问任何子视图( [alertView子视图] .count
将始终返回零)我们无法像以前那样自定义UIAlertViews。
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.
因此,在iOS 7下实现目标的唯一方法是构建一个类似于 UIAlertView
的自定义视图,然后根据需要自定义它。
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.
但是如果您在iOS 7之前编写iOS版本,那么您可以使用这个简单的黑客来访问按钮:
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
这样您就可以访问第一个按钮了。之后,您可以像往常一样自定义 UIAlertView
。
This way you would get access to the first button. After that you can customize your UIAlertView
as usual.
顺便说一下:Apple不仅希望通过改变我们的方式为所有 UIAlertView
提供相同的设计可以自定义它们。原因在于人机交互研究(人机交互)。如果这是在所有应用程序中实现的方式,人们倾向于认为底部按钮始终是默认答案。
此外,底部按钮是仅突出显示的按钮a UIAlertView
。因此,它的视觉重量比按钮的视觉重量更强,文本数量大致相同。这是人们倾向于选择这一个的另一个因素。这也是为什么突出显示的按钮永远不应该导致灾难性和不可逆转的行为('你想删除所有已保存的游戏'应该总是突出显示按钮'保存我保存的游戏'而不是那个告诉'删除所有内容')。< br>
因此,无论您添加按钮的顺序如何,Apple总是将取消按钮
设置为底部。因此,如果您的应用程序没有使用完全自定义界面并使用Apple提供的许多用户界面元素,我强烈建议您不要尝试更改该行为并将底部按钮设置为默认按钮。
By the way: Apple did not only want to give all UIAlertView
s 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.
这篇关于突出显示UIAlertView中的顶部按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!