Buttons are an easy way to provide interactivity to your app. These are views that run code when tapped. The good thing about buttons is that the button itself is just another view. For example, you can use an image or text. This is part of the flexibility of buttons.
Buttons in code look like the following:
Button {
// Swift code
} label: {
// View
}
As you can see, the Button looks like a little different than other views. It requires a few curly braces. These are areas where you can provide lots of code.
In this case, this produces a text button. Once the user taps the button, the message is printed to the console. Notice the Text view is being used for the label. You can provide an image or compose an entirely new view with accompanying modifiers.
See forum comments
This content was released on Apr 24 2025. The official support period is 6-months
from this date.
Learn about adding buttons to your SwiftUI app
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.