iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔥

How to Create an automatic-style ViewModifier in SwiftUI

に公開

I'll introduce how to create a .automatic modifier in SwiftUI that automatically switches styles based on the OS version.

What is an Automatic Style

In SwiftUI's official API, many components offer an .automatic style. For example, ButtonStyle.automatic, ToggleStyle.automatic, PickerStyle.automatic, etc. It's a mechanism where the system automatically selects the best style according to the current environment.

With this mechanism, developers can always apply the latest and most appropriate design without worrying about minor OS version differences. However, for custom styles, you need to implement a similar mechanism yourself.

Implementation

1. Basic Structure

Using a clock view style as an example, define the style based on a protocol.

protocol ClockStyle: ViewModifier {}

2. Define Individual Styles

// For iOS 26 and earlier - simple text display
struct ClassicClockStyle: ClockStyle {
    func body(content: Content) -> some View {
        content
            .background()
    }
}

// For iOS 26+ - glass effect
@available(iOS 26.0, *)
struct ModernClockStyle: ClockStyle {
    func body(content: Content) -> some View {
        content
            .glassEffect()
    }
}

3. Implement Automatic Style

SwiftUI doesn't have a class like ConditionalContent for ViewModifier, so it's recommended to branch inside body.

struct AutomaticClockStyle: ClockStyle {
    func body(content: Content) -> some View {
        if #available(iOS 26.0, *) {
            content.clockStyle(.modern)
        } else {
            content.clockStyle(.classic)
        }
    }
}

This checks the OS version both at compile time and runtime, selecting the optimal style.

4. Provide an Easy-to-Use API

As it is, you can only use it like clockStyle(ModernClockStyle()). To enable shorthand notation, design the API consciously. By providing an interface exactly identical to SwiftUI's official style system, you minimize the learning curve.

extension ClockStyle where Self == AutomaticClockStyle {
    static var automatic: Self { .init() }
}

extension ClockStyle where Self == ClassicClockStyle {
    static var classic: Self { .init() }
}

@available(iOS 15.0, *)
extension ClockStyle where Self == ModernClockStyle {
    static var modern: Self { .init() }
}

extension View {
    func clockStyle(_ style: some ClockStyle) -> some View {
        modifier(style)
    }
}

// Usage example
ClockView()
    .clockStyle(.automatic)

Benefits and Convenience

Improved Development Efficiency

Previously, you had to write individual style branches for each OS version. Using the .automatic pattern, you only need to implement once. This avoids code duplication and significantly reduces development time.

// Traditional manual switching (repeated in multiple places)
if #available(iOS 15.0, *) {
    ClockView()
        .glassEffect()
} else {
    ClockView()
        .background()
}

// Automatic pattern (managed in one place)
ClockView()
    .clockStyle(.automatic)

Summary

The .automatic modifier that automatically switches styles based on the OS version is a powerful mechanism that combines future-proofing, maintainability, and consistency. It is especially valuable in applications that require long-term maintenance.

Discussion