How to make a custom SwiftUI modifier

Example of a custom modifier. How to make modifier extension to call it natively.

Help in our Twitter Community for iOS developers

Reads in 2 minutes Updated 1 year ago

Create Modifier

There is a built-in tool for custom modifiers — you need to create a structure and implement the ViewModifier protocol. The protocol should be used to implement the body method and return a new View.

To give an example, let's make a modifier that combines styles for text:

struct LargeTitleModifier: ViewModifier {

    func body(content: Content) -> some View {
        content
            .font(.largeTitle)
            .foregroundStyle(.primary)
    }
}

You can use other modifiers and even embed View.

Apply Modifier

Call via .modifier and pass a custom modifier:

Text("Hello World")
    .modifier(LargeTitleModifier())

Native Style

In order for the modifier to be called natively, you need to make an extension for View:

extension View {

    func largeTitleStyle() -> some View {
        modifier(LargeTitleModifier())
    }
}

To narrow down the availability of the modifier, you can make the extension only for Text.

The call will now be in native style:

Text("Hello World")
    .largeTitleStyle()
We try to translate the tutorial well, but we aren't native. We appreciate if you can submit a Pull Request with fixes