How do you create a toolbar item using the standard system close button or prominent done (✔️) button in SwiftUI?
In UIKit UIBarButtonItem provides .close and .done system items, and to get the tinted checkmark for done you set style = .prominent.
SwiftUI provides a similar set of features. To get the standard system buttons, a new Button
initialiser allows you to create a button with a role and action, and provides a default label for you.
You can get the system button for any of these button roles: .cancel
, .destructive
, .close
, .confirm
. And you can use them like this:
// Standard close button
Button(role: .close) {
...
}
// Standard done button (prominent in toolbar)
Button(role: .confirm) {
...
}
// Prominent tinted button
.buttonStyle(.borderedProminent)