iTranslated by AI

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

Clean OS-Version Conditional Modifiers in SwiftUI

に公開2件

As OS updates are released, the number of available modifiers also continues to grow. While this is convenient, if you wanted to apply a recently introduced modifier, such as "Liquid Glass," to an existing app, you might end up with something like this:

import SwiftUI

if #available(iOS 26.0, *) {
  Button {
    // do some action
  } label: {
    Text("Do some action")
  }
  .glassEffect()
} else {
  Button {
    // do some action
  } label: {
    Text("Do some action")
  }
}

It feels like this is how it would look (right? right??). However, this causes two versions of the View to appear in many places, leading to redundancy and poor maintainability. That is why techniques like the following are commonly used:

import SwiftUI

extension View {
  @ViewBuilder
  func glassEffectIfOver26() -> some View {
    if #available(iOS 26.0, *) {
      self
        .glassEffect()
    } else {
      self
    }
  }
}

Button {
  // do some action
} label: {
  Text("Do some action")
}
.glassEffectIfOver26()

While this feels cleaner, there is a risk of an infinite proliferation of similar extensions. Fundamentally, you would ideally want to write it like this:

Button {
  // do some action
} label: {
  Text("Do some action")
}
if #available(iOS 26.0, *) {
  .glassEffect()
}

As of now (end of December 2025), due to SwiftUI's syntax, you cannot write #available directly in the middle of a View modifier chain. Therefore, I came up with and implemented the following extension:


extension View {
  /// Conditionally applies modifiers based on the current OS version.
  ///
  /// SwiftUI does not allow writing `#available` checks directly inside a
  /// modifier chain. `osCondition(_:)` acts as an escape hatch that enables
  /// OS-specific branching while keeping the modifier-style syntax.
  ///
  /// This is especially useful when adopting new system-provided modifiers
  /// (such as those introduced in newer iOS releases) without duplicating views
  /// or creating many one-off extension methods.
  ///
  /// Example:
  /// ```swift
  /// Text("Hello")
  ///   .osCondition {
  ///     if #available(iOS 26.0, *) {
  ///       $0.glassEffect()
  ///     } else {
  ///       $0
  ///     }
  ///   }
  /// ```
  ///
  /// - Parameter modifier: A closure that receives the current view and returns
  ///   a modified view. The closure is evaluated unconditionally, so OS version
  ///   checks should be performed inside using `#available`.
  @ViewBuilder public func osCondition<Content: View>(
    @ViewBuilder modifier: (Self) -> Content
  ) -> some View {
    modifier(self)
  }
}

This is a simple implementation that just executes the closure passed as an argument. However, you can use it like this:

Button {
  // do some action
} label: {
  Text("Do some action")
}
.osCondition { view in
  if #available(iOS 26.0, *) {
    view
      .glassEffect()
  } else {
    view
  }
}

Huh? Isn't this fine? Thinking that, I looked for similar solutions and found the following library:

https://github.com/Aeastr/Conditionals

The concept is similar. If you want a more feature-rich and standardized solution, using a library is a good option. Personally, however, for cases where I just want to perform a small OS branch, I feel this approach strikes a good balance. If anyone knows a "better way to write this," please let me know!

Discussion