Skip to content
项目

关于

SwiftUI-Backports 让你在支持旧版 iOS 的同时,也能轻松使用最新的 SwiftUI 修饰符,例如 iOS 26 引入的修饰符。只需使用简单的 `.backport` 修饰符,就能编写现代 SwiftUI 代码,无需为每个新 API 都添加 `#available` 检查。它轻量易集成,让 UI 代码保持整洁,并能适应未来变化。

网站与应用开发

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

backporthero

SwiftUI-Backports

✨ A lightweight way to use the latest SwiftUI modifier APIs while gracefully supporting older iOS versions.

Solving the N-1 Problem

Every fall, a new version of iOS ships with shiny new SwiftUI APIs. However, a lot of apps will continue to support the last version of iOS (i.e., N-1). That often leads to code that looks like this:

if #available(iOS 26.0, *) {
    SomeView().glassEffect()
} else {
    SomeView()
}

Multiply that across your entire codebase, and suddenly you’re tangled in #available checks and duplicated code.

The Solution

This project offers a simple Backport type that lets you use new SwiftUI APIs with a fallback — without cluttering your UI code.

  • ✅ Write modern SwiftUI code that reads cleanly
  • ✅ Seamlessly backport newer iOS modifiers to earlier versions
  • ✅ No more nested if #available blocks
  • ✅ Just one Swift file — easy to drop in and maintain

It’s based on Dave DeLong’s elegant approach to backwards compatibility in Swift. Definitely read that before diving in.

Example

Instead of writing:

if #available(iOS 26.0, *) {
    MyView().glassEffect()
} else {
    MyView()
}

You can write:

MyView()
    .backport.glassEffect()

And the Backport type handles the fallback internally. As such, it removes all the #available clutter from your call sites and centralize the fallback, while still using runtime availability branching under the hood. Here, the left side is using a thin material, while the right uses a glass effect on iOS 26 and above:

example

Installation

Swift Package Manager (Recommended)

You can add this repo as a dependency in Xcode:

  1. Open your project.
  2. Go to File > Add Packages…
  3. Enter the URL: https://github.com/superwall/iOS-Backports
  4. Choose the latest version and add it to your app target.

Then, import the package:

import SwiftUIBackports

struct MyView: View {
    var body: some View {
        AwesomeView()
            .backport.glassEffect()
    }
}

Manual

Just copy the Backport.swift file into your project. That’s it.

Backported Modifiers

iOS Version Modifier Description
iOS 17.0 contentTransition(_:) Applies a basic or numeric content transition
iOS 18.0 matchedTransitionSource(id:in:) Marks a view as a matched transition source
iOS 18.0 presentationSizeForm() Applies .presentationSizing(.form)
iOS 18.0 zoom(sourceID:in:) Applies a zoom navigation transition
iOS 18.1 imagePlayground(_:completion:) Presents an image playground sheet
iOS 18.0 widgetAccentedRenderingMode(_:) Sets how an Image should render in widget accented mode
iOS 26.0 backgroundExtensionEffect() Extends background beyond safe areas
iOS 26.0 glassButtonStyle() Applies the glass button style
iOS 26.0 glassEffect(_:in:) Applies a glass effect
iOS 26.0 glassEffect(_:in:fallback:) Glass effect with fallback background
iOS 26.0 glassEffectContainer(spacing:) Embed in a GlassEffectContainer
iOS 26.0 glassEffectID(_:in:) Tags glass views for matched animations
iOS 26.0 glassEffectTransition(_:) Animates glass transitions
iOS 26.0 glassEffectUnion(_:namespace:) Unites multiple glass effects by ID in a namespace
iOS 26.0 glassProminentButtonStyle() Applies the glass prominent button style
iOS 26.0 listSectionMargins(_:_: ) Sets margins for list sections
iOS 26.0 presentationBackground(in:) Applies a fallback background on earlier versions
iOS 26.0 scrollEdgeEffectHidden(_:for:) Hides scroll edge effects
iOS 26.0 scrollEdgeEffectStyle(_:for:) Customizes scroll view edge effects
iOS 26.0 symbolColorRenderingMode(_:) Sets symbol image rendering mode
iOS 26.0 symbolVariableValueMode(_:) Sets variable value rendering mode
iOS 26.0 tabBarMinimizeBehavior(_:) Sets the tab bar minimize behavior
iOS 26.0 safeAreaBar(edge:alignment:spacing:content:) Shows the specified content as a custom bar above or below the modified view
iOS 26.0 tabViewBottomAccessory(content:) Places a view as the bottom accessory of the tab view.
iOS 26.0 searchToolbarBehavior(_:) Configures the behavior for search in the toolbar.
iOS 26.0 sharedBackgroundVisibility(_:) Controls the visibility of the glass background effect on items in the toolbar.

Contributing

Got a new SwiftUI modifier you'd like to backport? Open a PR or file an issue — contributions are welcome!

License

MIT


Made with ❤️ by Superwall

探索相关项目

加载中…