iOS UIAlertController 粗体按钮在 8.3 中更改

iOS UIAlertController bold button changed in 8.3

UIAlertController 带有两个设置了样式的按钮:

UIAlertActionStyle.Cancel
UIAlertActionStyle.Default

在iOS8.2中,取消按钮是非粗体的,默认是粗体的。 在 iOS 8.3 中他们已经切换

你可以看到苹果自己的应用程序,例如设置>邮件>添加帐户> iCloud>输入无效数据,然后在8.3上显示如下:

不支持的 Apple ID

了解更多(粗体) OK(非粗体)

而 8.2 则相反。

任何使它再次像 8.2 的解决方法。为什么改变了?

我刚签入 iOS 8.2:第一个 添加的按钮是非粗体的,第二个 添加的按钮是大胆的。使用此代码,取消按钮将变为粗体:

[alertController addAction:[UIAlertAction actionWithTitle:@"Ok"
                                                    style:UIAlertActionStyleDefault
                                                  handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel"
                                                    style:UIAlertActionStyleCancel
                                                  handler:nil]];

使用此代码,默认按钮将变为粗体:

[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel"
                                                    style:UIAlertActionStyleCancel
                                                  handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok"
                                                    style:UIAlertActionStyleDefault
                                                  handler:nil]];

我现在无法签入 iOS 8.3,但这种行为可能是一个原因。

这是对 SDK 的有意更改。我刚刚收到 Apple 就此问题对 this radar 的回复,指出:

This is an intentional change - the cancel button is to be bolded in alerts.

不幸的是,我在各种更改日志中找不到任何提及此内容的内容。

因此,我们需要在某些地方对我们的应用进行更改,使某些事情变得有意义。

从 iOS 9 开始,您可以将 preferredAction 值设置为您希望按钮标题为粗体的操作。

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    let OKAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alert.addAction(cancelAction)
    alert.addAction(OKAction)
    alert.preferredAction = OKAction
    presentViewController(alert, animated: true) {}

右侧的确定按钮将以粗体显示。

自 iOS 9 起,UIAlertController 有一个名为 preferredAction 的 属性。 preferredAction 有以下声明:

var preferredAction: UIAlertAction? { get set }

The preferred action for the user to take from an alert. [...] The preferred action is relevant for the UIAlertController.Style.alert style only; it is not used by action sheets. When you specify a preferred action, the alert controller highlights the text of that action to give it emphasis. (If the alert also contains a cancel button, the preferred action receives the highlighting instead of the cancel button.) [...] The default value of this property is nil.


下面的 Swift 5 / iOS 12 示例代码显示了如何使用 [=13= 显示将突出显示指定 UIAlertAction 的文本的 UIAlertController ]:

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "OK", style: .default, handler: { action in
    print("Hello")
})

alertController.addAction(cancelAction)
alertController.addAction(okAction)
alertController.preferredAction = okAction

present(alertController, animated: true, completion: nil)

关于 objective-c 和 preferredAction for alertActions 的一些话。如果您使用 preferredAction,则 BOUTH alertAction 必须设置为 style:UIAlertActionStyleDefault。如果将某些设置为 style:UIAlertActionStyleCancel,preferredAction 将被忽略

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"All you base"
                                                                         message:@"Are belong to us!" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* alertActionShowYes = [UIAlertAction actionWithTitle:@"YES!" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    NSLog(@"I serve for my emperor!");
}];

UIAlertAction* alertActionNo = [UIAlertAction actionWithTitle:@"NO!" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    NSLog(@"NOOOO it's not true!!");
}];

[alertController addAction:alertActionShowYes];
[alertController addAction:alertActionNo];

alertController.preferredAction = alertActionShowYes;
[alertController setPreferredAction:alertActionShowYes];
    
[self presentViewController:alertController animated:YES completion:nil];