Use of custom view modifier for reusability in SwiftUI

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

Custom view modifier is very powerful concept in SwiftUI which will allow you to write reusable code. If you are using same kind of modifiers throughout multiple of views, it is better to create a custom view modifier. The main benefit of custom view modifier is that it will help you to generate a good design system. Creating custom view modifiers promotes consistency, clarity, and quality in the SwiftUI application design process. It can also deliver significant competitive advantages for small team.

Conform the ViewModifier protocol when you want to create a reusable view modifier that you can apply to any view. In this example, you can notice that both buttons have the same set of modifiers(the only font difference is font) and it makes sense to reuse those modifiers.

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            Button("Primary Button") {}
                .font(.title)
                .padding()
                .background(Color.orange)
                .foregroundColor(.white)
                .cornerRadius(20.0)
                .shadow(radius: 10.0)

            Button("Secondary Button") {}
                .font(.title2)
                .padding()
                .background(Color.orange)
                .foregroundColor(.white)
                .cornerRadius(20.0)
                .shadow(radius: 10.0)
        }
    }
}

The example below combines several modifiers to create a new modifier called CustomButtonModifier.

struct CustomButtonModifier: ViewModifier {
    var font: Font = .title

    func body(content: Content) -> some View {
        return content
            .font(font)
            .padding()
            .background(Color.orange)
            .foregroundColor(.white)
            .cornerRadius(20.0)
            .shadow(radius: 10.0)
    }
}

// Use it with modifier keyword
Button("Primary Button") {}
  .modifier(CustomButtonModifier())
Button("Secondary Button") {}
  .modifier(CustomButtonModifier(font: .title2))

As you can see that I have passed font as a parameter for CustomButtonModifier to differentiate primary and secondary button. You can apply the custom view modifier using modifier(_:) keyword but I prefer to create a function using the name of the custom modifier inside the View extension. It makes the code more readable.

extension View {
    func customButtonModifier(font: Font = .title) -> some View {
        modifier(CustomButtonModifier(font: font))
    }
}

// Use it without modifier keyword
Button("Primary Button") {}
  .customButtonModifier()

Output

Use of custom view modifier for reusability in SwiftUI ishtiz.com
struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            Button("Primary Button") {}
                .customButtonModifier()
            Button("Secondary Button") {}
                .customButtonModifier(font: .title2)
        }
    }
}

struct CustomButtonModifier: ViewModifier {
    var font: Font = .title

    func body(content: Content) -> some View {
        return content
            .font(font)
            .padding()
            .background(Color.orange)
            .foregroundColor(.white)
            .cornerRadius(20.0)
            .shadow(radius: 10.0)
    }
}

extension View {
    func customButtonModifier(font: Font = .title) -> some View {
        modifier(CustomButtonModifier(font: font))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



Get Ready to Shine: Mastering the iOS Interview




Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.