52 - Add an Icon to a Button in SwiftUI

To add an icon to a button in SwiftUI, you can leverage SF Symbols, which is a built-in library of thousands of icons provided by Apple. SF Symbols make it easy to enhance the visual appeal and functionality of your buttons.

struct ContentView: View {
  var body: some View {
    VStack {
      Button(action: {
        // Action to perform when the button is tapped
      }) {
        Label("Show Some Love!", systemImage: "heart.fill")
          .padding()
          .foregroundColor(.white)
          .background(Color.blue)
          .cornerRadius(10)
      }
    }
  }
}

Your preview should look like this:

Add an icon to your button with SF Symbols.

Here’s how this code works:

By using SF Symbols, you can easily enhance the visual appeal and usability of your buttons in SwiftUI. The extensive library of icons available through SF Symbols ensures that you can find the perfect icon to represent various actions and concepts in your app. Enjoy adding icons to your buttons and giving them a personalized touch!