SwiftUI - Button



Button view in SwiftUI is the most powerful and commonly used UI control. A button control performs a specified action when the user taps or clicks on it. Or we can say it triggers the action when the user interacts with it by clicking on it.

It can be used to perform various actions such as submitting forms, initiating navigation, enabling/disabling, or any user-defined action. We can also use a button inside any container, such as a list, menus, etc. So, in this chapter, we are going to discuss how to create and style buttons.

Creating Button in SwiftUI

A button is used to act when we click on it. It is the most versatile UI control in SwiftUI because we can style it variously so that it can easily fit in the UI design. A button can be text, image, or shape with the label.

So, to create a button, SwiftUI provide a pre-defined method or modifier name Button(). It creates a button with an action. So whenever we click on it, then it will trigger that action.

Syntax

Following is the basic syntax of button −

Button(
   _ title key: LocalizedStringKey,
   action: @escaping @MainActor () -> Void
)

Parameters

Following parameters are used by this view −

  • titleKey: Represent the title of the button.

  • action: Represent the action that the button will perform when the user clicks on it.

Overloaded Button() Methods

Following are some overloaded methods of Button −

Button Description
Button(action()->, label:()->Label) It is used to create a button with a custom label.
Button(_:action:) It is used to create a button whose label is generated from the given string.
Button(_:image:action:) It is used to create a button whose label is generated from a string and image.
Button(_:systemImage:action:) It is used to create a button whose label is generated from a string and system image.
Button(_:role:action:) It is used to create a button with a specified role, and the label of the button is generated from the string.

Example 1

The following SwiftUI program to create a simple button.

import SwiftUI
struct ContentView: View {
   var body: some View {
      VStack{
         Button("Submit", action: submit).font(.largeTitle)
      }
   }
   func submit(){
      print("Form is submitted")
   }
}
#Preview {
   ContentView()
}

Output

Button

Example 2

The following SwiftUI program to create a different type of buttons.

import SwiftUI

struct ContentView: View {
   var body: some View {
      VStack{
         
         // Simple button with action
         Button("Submit", action: submit)
            .font(.largeTitle)
         // Nutton with inline action and label
         Button(action: {print("Submitted")}, label: {
            Text("Button").font(.largeTitle)
         })
         
         // Button with role
         Button("Cancel", role: .cancel, action: {print("Deleted")}).font(.largeTitle)
      }
   }
   func submit(){
      print("Form is submitted")
   }
}
#Preview {
   ContentView()
}

Output

Button

Styling Button in SwiftUI

SwiftUI provides a pre-defined modifier named buttonStyle() to style the button. This modifier allows users to apply custom as well as standard styles to enhance the appearance of the specified button. We can also create custom styles to style the button; these custom styles should conform to the ButtonStyle protocol.

Syntax

Following is the syntax −

func buttonStyle(Style)

Example

The following SwiftUI program to create a button with a custom style.

import SwiftUI

// Custom Style for the button
struct customStyle: ButtonStyle{
   func makeBody(configuration: Configuration) -> some View {
      configuration.label
         .background(configuration.isPressed ? .yellow : .red)
         .foregroundStyle(.white)
         .clipShape(Capsule())
         .padding()
         .shadow(radius: 14)
         .scaleEffect(configuration.isPressed ? 0.99 : 1.0)
   }
}
struct ContentView: View {
   var body: some View {
      VStack{
         
         // Simple button with action
         Button("Submit", action: submit)
            .font(.largeTitle)
         
         // Button with style
         Button("Submit", action: submit)
            .font(.largeTitle)
            .buttonStyle(customStyle())
      }
   }
   func submit(){
      print("Form is submitted")
   }
}
#Preview {
   ContentView()
}

Output

Button

Button with image in SwiftUI

In SwiftUI, we are allowed to used the image as a button. We can use any type of image as a button such as jpeg, png, etc., We can also create an icon as a button or can use SF symbol icon as a button.

Example 1

The following SwiftUI program to create buttons with images.

import SwiftUI

struct ContentView: View {
   var body: some View {
      VStack{
         
         // Image as a button
         Button(action: {print("Display")}){
            Image("Image").frame(width: 150).clipShape(Circle())
         }
      }
   }

#Preview {
   ContentView()
}

Output

Button

Example 2

The following SwiftUI program to create icon as button.

import SwiftUI
struct ContentView: View {
   var body: some View {
      VStack{
         
         // SF symbol icon as a button
         Button(action: {print("Display")}){
            Image(systemName: "doc.fill").font(.largeTitle)
         }
         
         // SF symbol icon as a button with text
         Button(action: {print("Display")}){
            HStack{
               Image(systemName: "doc.fill").font(.largeTitle)
               Text("Open")
               
            }
         }
      }
   }
}
#Preview {
   ContentView()
}

Output

Button

Animated Button in SwiftUI

In SwiftUI, we are also allowed to animate button. To create animated button we have to apply animation using .animation() modifier. This animation() modifier applies different type of animations such as easeIn, easeOut, etc on the given button.

Example

The following SwiftUI program to create an animated button.

import SwiftUI
struct ContentView: View {
   @State private var isPressed = false
   var body: some View {
      VStack{
         Button("Submit", action: submit)
            .font(.largeTitle)
            .scaleEffect(isPressed ? 0.8 : 1.2)
            .animation(.easeIn, value: isPressed)
      }
   }
   func submit(){
      print("Form is submitted")
   }
}

#Preview {
   ContentView()
}

Output

Button
Advertisements