iOS UIAlertController bold button changed in 8.3

被刻印的时光 ゝ 提交于 2019-11-28 17:44:59

From iOS 9 you can set the preferredAction value to the action which you want the button title to be bold.

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

The OK button which is on the right will be in bold font.

This is an intentional change to the SDK. I have just had a response from Apple to this radar on the issue, stating that:

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

I can't find anything in the various change logs mentioning this, unfortunately.

So, we'll need to make changes to our apps in places to make some things make sense.

I just checked in iOS 8.2: a first added button is non-bold and a second added button is bold. With this code a cancel button will be bold:

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

And with this code a default button will be bold:

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

I can't check in iOS 8.3 now but this behavior can be a reason.

Since iOS 9, UIAlertController has a property called preferredAction. preferredAction has the following declaration:

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.


The Swift 5 / iOS 12 sample code below shows how to display a UIAlertController that will highlight the text of a specified UIAlertAction using preferredAction:

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!