Using Buttons in Swift

Learn iOS Programming

Butttons are one of the most used user interface elements in SwiftUI. A Basic button is defined as follows.

In above code, Text on button is defined in brackets as “Delete”. Action to be executed after button press is defined in parenthesis.

Button with Function

When a separate function is created for button action, button is defined as follows.

Assigning Roles to Buttons

Buttons can be assigned roles to change their appearance as per role. Example is destructive role such as delete. In this case, button is shown in red color.

Adding Styles to Buttons

Styles can be added to buttons as modifiers.

Adding Tint to Buttons

Color of button can be changed using tint modifier as follows.

Button with Image Icon

Icons can be added to button using trailing closures like label.