iphone - 突出显示 UIAlertView 中的顶部按钮

标签 iphone ios uialertview

我有一个 UIAlertView,其中 3 个按钮默认在 UIAlertView 中垂直显示。我希望顶部按钮加粗/突出显示。根据我的理解和测试,“取消”按钮是突出显示的按钮。问题是无论我如何设置取消按钮,它都放在这一行的最后。我无法让它成为第一个按钮。

我试过明确设置取消按钮

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

以及设置取消按钮的索引

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 subviews] 来访问 UIAlertView 的 subview .但是由于 iOS 7 不允许我们访问任何 subview ([alertView subviews].count 将始终返回零)我们无法像过去那样自定义 UIAlertViews。

因此,在 iOS 7 下实现您的目标的唯一方法是构建一个看起来像 UIAlertView 的自定义 View ,然后根据需要对其进行自定义。

但是如果您正在为 iOS 7 之前的 iOS 版本编码,那么您可以使用这个简单的 hack 来访问按钮:

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

这样您就可以访问第一个按钮。之后,您可以像往常一样自定义您的 UIAlertView

顺便说一句:Apple 不仅希望通过改变我们可以自定义它们的方式来为所有 UIAlertView 提供相同的设计。原因在于HCI研究(人机交互)。人们倾向于认为底部按钮始终是“默认”答案,如果这是在所有应用程序中实现的方式的话。
此外,底部按钮是 UIAlertView唯一 突出显示的按钮。所以它的视觉重量比具有大约相同数量文本的按钮的视觉重量更强。这是人们倾向于选择这个的另一个因素。这也是为什么突出显示的按钮永远不会导致灾难性和不可逆转的行为的原因(“你想删除所有保存的游戏”应该始终突出显示按钮“保留我保存的游戏”而不是告诉“删除所有内容”的按钮)。< br/> 因此无论您以何种顺序添加按钮,Apple 始终将取消按钮 置于底部。因此,如果您的应用不使用完全自定义的界面并使用 Apple 提供的许多用户界面元素,那么我强烈建议您不要尝试更改该行为并将底部按钮设为“默认”按钮。

关于iphone - 突出显示 UIAlertView 中的顶部按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19125249/

相关文章:

iphone - 视频播放失败 - [NSURL initFileURLWithPath :]: nil string parameter

ios - Apple 推送通知 : Not receiving device token?

iphone - NSString stringWithCharacters Unicode 问题

iphone - 允许经过身份验证的 IOS Game Center 用户自动进行身份验证

iphone - UIAlertView 无法显示并导致 “EXC_BAD_ACCESS” 错误

ios - FBSDKLikeControl 变灰/禁用

android - SQLite Xamarin 重复列 ID 问题

ios - 如何在 swift 中从字节数组创建位图图像

ios - 我如何添加带有事件指示器的时尚白色 uialertview

ios - 检测系统警报 View 何时出现/将关闭