63

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Bbx
  • 3,184
  • 3
  • 22
  • 33

5 Answers5

146

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.

Thi
  • 2,066
  • 1
  • 17
  • 12
16

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.

Josh Heald
  • 3,907
  • 28
  • 37
12

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)
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
2

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.

Vlad
  • 7,199
  • 2
  • 25
  • 32
  • 2
    Thanks for the idea. I tried it but it makes no difference in 8.3. I think it must be an Apple bug as it looks wrong in their alerts. Eg the Push Notifications alert now has **Don't Allow** (bold) OK (non bold) which is surely not what anyone wants... – Bbx Apr 14 '15 at 08:39
  • It must be a bug for system alerts, but you can add your buttons (actions) in the right sequence for iOS 8.2 (8.3?) or set a right style for action if the action's style is a reason of a bold style. – Vlad Apr 14 '15 at 09:19
  • Agreed. This is very odd. Order doesn't make any difference, why is no the bold type? – netwire Aug 11 '15 at 04:08
  • I would assume no change is by default "correct" - and therefore bold. I assume Apple wants to nudge users to be more critical to blindly allowing changes. – Johan Mar 29 '16 at 09:40
0

Some words about objective-c and preferredAction for alertActions. If you use preferredAction you BOUTH alertAction must set as style:UIAlertActionStyleDefault. If some one will be set as style:UIAlertActionStyleCancel, preferredAction will be ignored

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];