iOS 8.3中UIAlertController的粗体按钮发生了变化

64

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

UIAlertActionStyle.Cancel
UIAlertActionStyle.Default
在iOS 8.2中,“取消”按钮是非粗体,“默认”是粗体。在iOS 8.3中,它们进行了交换。
您可以在Apple自己的应用程序中看到它,例如:设置>邮件>添加帐户>iCloud>输入无效数据,然后在8.3上显示如下:
不支持的 Apple ID
了解更多(粗体) 确定(非粗体)
而在8.2中则相反。
有没有任何方法可以使其再次像8.2那样?为什么它会改变?
5个回答

147

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) {}

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


2
请注意,这仅适用于 UIAlertControllerStyle.Alert(而不是操作表)。 - Robert

16

这是SDK故意进行的更改。我刚刚收到了苹果对于此问题的回应,他们指出:

这是有意进行的更改 - 取消按钮将会在警告框中加粗显示。

很遗憾,在各种变更日志中我找不到任何提及此更改的内容。

因此,我们需要在应用程序的某些位置进行更改以使一些事情更容易理解。


12

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

var preferredAction: UIAlertAction? { get set }
下面是Swift 5 / iOS 12的示例代码,展示了如何显示一个UIAlertController,使用preferredAction来突出显示指定的UIAlertAction的文本。首选操作仅适用于UIAlertController.Style.alert样式; 它不适用于操作表。当您指定首选操作时,警报控制器会突出显示该操作的文本以强调它。(如果警报还包含取消按钮,则首选操作会接收突出显示而不是取消按钮。)这个属性的默认值是nil
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)

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上进行检查,但这种行为可能是一个原因。


2
谢谢你的建议。我尝试了,但在8.3版本中没有任何区别。我认为这一定是苹果的一个bug,因为他们的警告看起来很奇怪。例如,推送通知警告现在有“不允许”(加粗)和“确定”(非加粗),这显然不是任何人想要的... - Bbx
这可能是系统警报的一个错误,但你可以按正确的顺序为iOS 8.2(8.3?)添加按钮(操作),或者如果操作的样式是粗体样式的原因,则设置正确的操作样式。 - Vlad
同意。这很奇怪。顺序没有任何影响,为什么不是粗体? - netwire
我认为默认情况下没有更改是“正确”的 - 因此是粗体的。我认为苹果希望促使用户更加批判地对待盲目允许更改。 - Johan

0
关于Objective-C和alertActions的preferredAction,有一些需要注意的地方。如果您使用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];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接