突出显示UIAlertView中的顶部按钮

Kev*_*_TA 7 iphone uialertview ios

我有一个UIAlertView,UIAlertView默认垂直显示3个按钮.我希望顶部按钮是粗体/突出显示.根据我的理解和测试,"取消"按钮是突出显示的按钮.问题是无论我如何设置取消按钮,它都放在此行的最后.我不能让它成为第一个按钮.

我已经尝试明确设置取消按钮

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                message:message
                                               delegate:self
                                      cancelButtonTitle:@"Top Button"
                                      otherButtonTitles:@"Middle Button", @"Bottom Button", nil];
Run Code Online (Sandbox Code Playgroud)

以及设置取消按钮的索引

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                       message:message
                                                      delegate:self
                                             cancelButtonTitle:nil
                                             otherButtonTitles:@"Top Button", @"Middle Button", @"Bottom Button", nil];
alert.cancelButtonIndex = 0;
Run Code Online (Sandbox Code Playgroud)

Jör*_*hof 8

这个问题实际上是由Apple在iOS 7中所做的更改引起的.在iOS 7之前,我们能够UIAlertView通过调用访问一个子视图[alertView subviews].但由于iOS 7不允许我们访问任何子视图([alertView subviews].count总是返回零),我们无法像以前那样自定义UIAlertViews.

因此,在iOS 7下实现目标的唯一方法是构建一个自定义视图,UIAlertView然后根据需要自定义它.

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

UIAlertView *alertView = [[UIAlertView alloc] init];
[alertView addButtonWithTitle:@"Yes"];
UIButton *yesButton = [alertView.subviews lastObject]; //is nil under iOS 7
Run Code Online (Sandbox Code Playgroud)

这样您就可以访问第一个按钮.之后,您可以UIAlertView照常自定义.

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