Preferred Actions in Alerts

Is there any way to set preferred actions for alerts in SwiftUI?

You can set .cancel roles for alert buttons, but they appear in bold and I want the other preferred action to be in bold. I could set the preferred action to have the .cancel role, but I don't think you're supposed to do it like that.

I haven't found a proper way yet, but I haven't played around with alerts enough to know.

Thanks for any suggestions.

Answered by BabyJ in 683467022

It seems that, after rereading the new documentation on alert(_:ispresented:actions:message:), you can just add the .keyboardShortcut(.defaultAction) modifier to one of the buttons in the alert's actions to make it the default action and therefore bold.

Not sure this is helpful, but the HIG for Alerts says the cancel button should be the bold one:

"Identify destructive buttons. If an alert button results in a destructive action, such as deleting content, set the button’s style to Destructive so that it gets appropriate formatting by the system. For developer guidance, see the UIAlertActionStyleDestructive constant of UIAlertAction. Additionally, provide a Cancel button so people can safely opt out of the destructive action. Make the Cancel button bold by marking it as the default button."

Accepted Answer

It seems that, after rereading the new documentation on alert(_:ispresented:actions:message:), you can just add the .keyboardShortcut(.defaultAction) modifier to one of the buttons in the alert's actions to make it the default action and therefore bold.

Preferred Actions in Alerts
 
 
Q