Communitygithub.com

dimillian/swiftui-ui-patterns

构建 SwiftUI 视图和组件的最佳实践与示例驱动指南,涵盖导航层次结构、自定义视图修饰符以及使用堆栈和网格实现响应式布局。适用于创建或重构 SwiftUI UI、设计 TabView 标签架构、用 VStack/HStack 组合屏幕、管理 @State 或 @Binding、构建声明式 iOS 界面,或需要组件级模式和示例时。

swiftui-ui-patterns 是什么?

这个技能提供了构建 SwiftUI 视图和组件的全面最佳实践与示例驱动指南。它涵盖了 SwiftUI 开发的多个关键领域,包括导航层次结构(如 NavigationStack、NavigationLink)、自定义视图修饰符(ViewModifier)、以及使用 VStack、HStack 和网格(LazyVGrid/LazyHGrid)实现响应式布局。技能旨在帮助开发者遵循 SwiftUI 声明式范式,在 iOS/macOS 等 Apple 平台项目中创建可复用、易维护的用户界面,同时促进组件化设计与高效的状态管理。文档包含可运行的代码片段,可直接用于创建新界面或重构现有 UI。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/dimillian/skills/tree/main/skills/swiftui-ui-patterns

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

swiftui-ui-patterns 是做什么的?

构建 SwiftUI 视图和组件的最佳实践与示例驱动指南,涵盖导航层次结构、自定义视图修饰符以及使用堆栈和网格实现响应式布局。适用于创建或重构 SwiftUI UI、设计 TabView 标签架构、用 VStack/HStack 组合屏幕、管理 @State 或 @Binding、构建声明式 iOS 界面,或需要组件级模式和示例时。

概述

该技能提供了丰富的 SwiftUI 模式,帮助开发者高效构建 iOS 和 macOS 应用界面。涵盖以下关键领域:

  • 导航架构:使用 NavigationStack、NavigationLink 实现分层导航。
  • 视图修饰符:通过自定义 ViewModifier 复用样式逻辑。
  • 响应式布局:使用 VStack、HStack、ZStack 以及网格布局(LazyVGrid、LazyHGrid)组织内容。
  • 状态管理:利用 @State、@Binding、@ObservedObject 等管理数据流。
  • 组件模式:提供常见的 UI 组件实现,如标签栏(TabView)、列表(List)、表单(Form)、卡片等。

核心概念

1. 导航层次结构

在 SwiftUI 中,导航通过 NavigationStack 实现(iOS 16+)或使用 NavigationView(旧版本)。

struct ContentView: View {
    var body: some View {
        NavigationStack {
            List(items) { item in
                NavigationLink(item.name, value: item)
            }
            .navigationDestination(for: Item.self) { item in
                DetailView(item: item)
            }
            .navigationTitle("Items")
        }
    }
}

2. 自定义视图修饰符

创建可复用的修饰符以封装常见样式:

struct CardStyle: ViewModifier {
    func body(content: Content) -> some View {
        content
            .padding()
            .background(Color(.systemBackground))
            .cornerRadius(12)
            .shadow(color: .gray.opacity(0.3), radius: 5, x: 0, y: 2)
    }
}

extension View {
    func cardStyle() -> some View {
        modifier(CardStyle())
    }
}

3. 响应式布局

使用堆栈和网格创建灵活布局:

struct GridGallery: View {
    let columns = [
        GridItem(.adaptive(minimum: 100))
    ]
    
    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(0..<20) { index in
                    Rectangle()
                        .fill(Color.blue)
                        .frame(height: 100)
                        .overlay(Text("\(index)"))
                }
            }
            .padding()
        }
    }
}

4. 状态与绑定

管理视图内部状态和父子视图间数据传递:

struct ParentView: View {
    @State private var isOn = false
    
    var body: some View {
        ToggleView(isOn: $isOn)
    }
}

struct ToggleView: View {
    @Binding var isOn: Bool
    
    var body: some View {
        Toggle("Enable", isOn: $isOn)
    }
}

平台支持

该技能适用于所有 Apple 平台:iOS、iPadOS、macOS、watchOS 和 tvOS。示例代码使用 SwiftUI 最新语法,建议使用 Xcode 14+ 和 Swift 5.7+ 以获得最佳兼容性。

如何贡献

欢迎提交改进或新的模式示例。请遵循以下步骤:

  1. 确保示例代码简洁且可运行。
  2. 添加清晰的注释和说明。
  3. 保持格式与现有文档一致。

许可证

该项目采用 MIT 许可证。详见 LICENSE 文件。

Individual skills in this repo

This repo contains 7 individual skills — each has its own dedicated page.

dimillian/app-store-changelog

Create user-facing App Store release notes by collecting and summarizing all user-impacting changes since the last git tag (or a specified ref). Use when asked to generate a comprehensive release changelog, App Store "What's New" text, or release notes based on git history or tags.

dimillian/ios-debugger-agent

Use XcodeBuildMCP to build, run, launch, and debug the current iOS project on a booted simulator. Trigger when asked to run an iOS app, interact with the simulator UI, inspect on-screen state, capture logs/console output, or diagnose runtime behavior using XcodeBuildMCP tools.

dimillian/macos-spm-app-packaging

Scaffold, build, and package SwiftPM-based macOS apps without an Xcode project. Use when you need a from-scratch macOS app layout, SwiftPM targets/resources, a custom .app bundle assembly script, or signing/notarization/appcast steps outside Xcode.

dimillian/swift-concurrency-expert

Swift Concurrency review and remediation for Swift 6.2+. Use when asked to review Swift Concurrency usage, improve concurrency compliance, or fix Swift concurrency compiler errors in a feature or file. Concrete actions include adding Sendable conformance, applying @MainActor annotations, resolving actor isolation warnings, fixing data race diagnostics, and migrating completion handlers to async/await.

dimillian/swiftui-liquid-glass

Implement, review, or improve SwiftUI features using the iOS 26+ Liquid Glass API. Use when asked to adopt Liquid Glass in new SwiftUI UI, refactor an existing feature to Liquid Glass, or review Liquid Glass usage for correctness, performance, and design alignment.

dimillian/swiftui-performance-audit

Audit and improve SwiftUI runtime performance from code review and architecture. Use for requests to diagnose slow rendering, janky scrolling, high CPU/memory usage, excessive view updates, or layout thrash in SwiftUI apps, and to provide guidance for user-run Instruments profiling when code review alone is insufficient.

dimillian/swiftui-view-refactor

Refactor and review SwiftUI view files with strong defaults for small dedicated subviews, MV-over-MVVM data flow, stable view trees, explicit dependency injection, and correct Observation usage. Use when cleaning up a SwiftUI view, splitting long bodies, removing inline actions or side effects, reducing computed `some View` helpers, or standardizing `@Observable` and view model initialization patterns.

相关技能