UIAlertController with two buttons with styles set:
UIAlertActionStyle.Cancel
UIAlertActionStyle.Default
in iOS 8.2, the Cancel button is non-bold and Default is bold. In iOS 8.3 they have switched round
You can see it Apple's own apps e.g., Settings > Mail > Add Account > iCloud > enter invalid data, then it shows like this on 8.3:
Unsupported Apple ID
Learn More (bold) OK (non-bold)
whereas it was the other way round for 8.2.
Any workaround to make it like 8.2 again. Why has it changed?
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.
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 isnil
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With