Modifiers in SwiftUI

Modifiers in SwiftUI allows us to describe a customization for a view. We can use it directly to wrap a view or we can create an extension, which will apply the customization to a view, and return the view customize.


This is an example of how to create a view modifier and an extension that we can use to apply our modifier like any other SwiftUI modifier.

struct CornerButtonModifier: ViewModifier {

    func body(content: Content) -> some View {
        return content
            .shadow(color: .black, radius: 2, x: 2, y: 2)
            .padding(.horizontal, 36)
            .padding(.vertical, 8)
            .background(Color.secondaryColor)
            .foregroundColor(.white)
            .font(.title2)
            .cornerRadius(6)
    }
}

extension View {

    func regularBorderButton() -> some View {
        return modifier(CornerButtonModifier())
    }

}