swiftui

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SwiftUI Skill

SwiftUI 技能

When to Use This Skill

何时使用该技能

Use this skill when:
  • Building user interfaces with SwiftUI
  • Working with SwiftUI views, modifiers, and layouts
  • Implementing state management with @State, @Binding, @ObservableObject
  • Creating animations and transitions
  • Implementing navigation with NavigationStack, NavigationLink, TabView
  • Working with data flow patterns (@Environment, @EnvironmentObject)
  • Using SwiftUI controls (Button, TextField, Picker, etc.)
  • Building cross-platform apps for iOS, macOS, watchOS, tvOS, visionOS
在以下场景使用该技能:
  • 使用SwiftUI构建用户界面
  • 处理SwiftUI视图、修饰器和布局
  • 使用@State、@Binding、@ObservableObject实现状态管理
  • 创建动画与转场效果
  • 使用NavigationStack、NavigationLink、TabView实现导航
  • 处理数据流模式(@Environment、@EnvironmentObject)
  • 使用SwiftUI控件(Button、TextField、Picker等)
  • 为iOS、macOS、watchOS、tvOS、visionOS构建跨平台应用

Description

描述

Complete SwiftUI framework documentation covering views, modifiers, layout, state management, animations, navigation, data flow, and all SwiftUI APIs for iOS, macOS, watchOS, tvOS, and visionOS.
完整的SwiftUI框架文档,涵盖视图、修饰器、布局、状态管理、动画、导航、数据流,以及适用于iOS、macOS、watchOS、tvOS和visionOS的所有SwiftUI API。

Quick Reference

快速参考

Core Components

核心组件

Accessibility

无障碍访问

  • AccessibilityTraits
  • AccessibilityTraits

Animations

动画

  • Animation
  • AnyTransition
  • Transition
  • Animation
  • AnyTransition
  • Transition

Api Reference

API参考

  • Capsule
  • Circle
  • Commands
  • ConfirmationDialog
  • DisclosureGroup
  • Ellipse
  • ForEach
  • Form
  • FullScreenCover
  • Gauge
  • GeometryProxy
  • GeometryReader
  • GroupBox
  • ImmersiveSpace
  • List
  • Menu
  • MenuBarExtra
  • ObservedObject
  • OutlineGroup
  • Published
  • Rectangle
  • RoundedRectangle
  • Section
  • SecureField
  • SwiftUI
  • Toolbar
  • ToolbarItem
  • ToolbarItemGroup
  • Capsule
  • Circle
  • Commands
  • ConfirmationDialog
  • DisclosureGroup
  • Ellipse
  • ForEach
  • Form
  • FullScreenCover
  • Gauge
  • GeometryProxy
  • GeometryReader
  • GroupBox
  • ImmersiveSpace
  • List
  • Menu
  • MenuBarExtra
  • ObservedObject
  • OutlineGroup
  • Published
  • Rectangle
  • RoundedRectangle
  • Section
  • SecureField
  • SwiftUI
  • Toolbar
  • ToolbarItem
  • ToolbarItemGroup

App Structure

应用结构

  • DocumentGroup
  • Scene
  • Settings
  • WindowGroup
  • DocumentGroup
  • Scene
  • Settings
  • WindowGroup

Controls

控件

  • Button
  • DatePicker
  • Picker
  • Slider
  • Stepper
  • Toggle
  • Button
  • DatePicker
  • Picker
  • Slider
  • Stepper
  • Toggle

Data Flow

数据流

  • FocusedSceneValue
  • PreferenceKey
  • FocusedSceneValue
  • PreferenceKey

Drawing

绘图

  • Canvas
  • Path
  • Canvas
  • Path

Essentials

基础内容

  • App
  • AppStorage
  • App
  • AppStorage

Gestures

手势

  • DigitalCrownRotationalSensitivity
  • DragGesture
  • LongPressGesture
  • MagnificationGesture
  • RotationGesture
  • TapGesture
  • DigitalCrownRotationalSensitivity
  • DragGesture
  • LongPressGesture
  • MagnificationGesture
  • RotationGesture
  • TapGesture

Layout

布局

  • Divider
  • Grid
  • GridRow
  • HStack
  • LazyHGrid
  • LazyHStack
  • LazyVGrid
  • LazyVStack
  • NavigationStack
  • Spacer
  • VStack
  • ZStack
  • Divider
  • Grid
  • GridRow
  • HStack
  • LazyHGrid
  • LazyHStack
  • LazyVGrid
  • LazyVStack
  • NavigationStack
  • Spacer
  • VStack
  • ZStack

Navigation

导航

  • Alert
  • Link
  • NavigationLink
  • NavigationPath
  • Popover
  • Sheet
  • Table
  • TableColumn
  • WKInterfaceObjectRepresentable
  • Alert
  • Link
  • NavigationLink
  • NavigationPath
  • Popover
  • Sheet
  • Table
  • TableColumn
  • WKInterfaceObjectRepresentable

State Management

状态管理

  • Binding
  • Environment
  • EnvironmentObject
  • FocusState
  • Observable
  • ObservableObject
  • SceneStorage
  • State
  • StateObject
  • Binding
  • Environment
  • EnvironmentObject
  • FocusState
  • Observable
  • ObservableObject
  • SceneStorage
  • State
  • StateObject

Views

视图

  • AccessibilityLabel
  • Color
  • ColorPicker
  • ContextMenu
  • Image
  • Label
  • NSViewControllerRepresentable
  • NSViewRepresentable
  • PreviewProvider
  • ProgressView
  • RealityView
  • ScrollView
  • Shape
  • TabView
  • Text
  • TextEditor
  • TextField
  • TimelineView
  • UIViewControllerRepresentable
  • UIViewRepresentable
  • View
  • ViewModifier
  • AccessibilityLabel
  • Color
  • ColorPicker
  • ContextMenu
  • Image
  • Label
  • NSViewControllerRepresentable
  • NSViewRepresentable
  • PreviewProvider
  • ProgressView
  • RealityView
  • ScrollView
  • Shape
  • TabView
  • Text
  • TextEditor
  • TextField
  • TimelineView
  • UIViewControllerRepresentable
  • UIViewRepresentable
  • View
  • ViewModifier

Key Concepts

核心概念

Platform Support

平台支持

  • iOS 26.0+
  • iPadOS 26.0+
  • macOS 26.0+
  • Mac Catalyst 26.0+
  • visionOS 26.0+
  • iOS 26.0+
  • iPadOS 26.0+
  • macOS 26.0+
  • Mac Catalyst 26.0+
  • visionOS 26.0+

On-Device AI

设备端AI

All models run entirely on-device, ensuring privacy and offline capability.
所有模型完全在设备端运行,确保隐私性和离线使用能力。

Usage Guidelines

使用指南

  1. Check model availability before use
  2. Define clear instructions for the model's behavior
  3. Use guided generation for structured outputs
  4. Implement tool calling for dynamic capabilities
  5. Handle errors appropriately
  1. 使用前检查模型可用性
  2. 为模型行为定义清晰的指令
  3. 使用引导生成来获取结构化输出
  4. 实现工具调用以获得动态能力
  5. 妥善处理错误

Navigation

导航

See the
references/
directory for detailed API documentation organized by category:
  • references/accessibility.md
    - Accessibility
  • references/animations.md
    - Animations
  • references/api_reference.md
    - Api Reference
  • references/app_structure.md
    - App Structure
  • references/controls.md
    - Controls
  • references/data_flow.md
    - Data Flow
  • references/drawing.md
    - Drawing
  • references/essentials.md
    - Essentials
  • references/gestures.md
    - Gestures
  • references/layout.md
    - Layout
  • references/navigation.md
    - Navigation
  • references/state_management.md
    - State Management
  • references/view_modifiers.md
    - View Modifiers (18 modifiers including .sheet(), .frame(), .animation(), etc.)
  • references/views.md
    - Views
请查看
references/
目录下按类别组织的详细API文档:
  • references/accessibility.md
    - 无障碍访问
  • references/animations.md
    - 动画
  • references/api_reference.md
    - API参考
  • references/app_structure.md
    - 应用结构
  • references/controls.md
    - 控件
  • references/data_flow.md
    - 数据流
  • references/drawing.md
    - 绘图
  • references/essentials.md
    - 基础内容
  • references/gestures.md
    - 手势
  • references/layout.md
    - 布局
  • references/navigation.md
    - 导航
  • references/state_management.md
    - 状态管理
  • references/view_modifiers.md
    - 视图修饰器(包含.sheet()、.frame()、.animation()等18种修饰器)
  • references/views.md
    - 视图

Best Practices

最佳实践

  • Prompting: Be specific and clear in your prompts
  • Instructions: Define the model's behavior upfront
  • Safety: Enable guardrails for sensitive content
  • Localization: Check supported languages for your use case
  • Performance: Use prewarm() for better response times
  • Streaming: Use streamResponse() for real-time user feedback
  • 提示词: 提示词应具体清晰
  • 指令: 预先定义模型的行为
  • 安全性: 为敏感内容启用防护机制
  • 本地化: 检查你的使用场景所支持的语言
  • 性能: 使用prewarm()以获得更快的响应速度
  • 流式传输: 使用streamResponse()以实现实时用户反馈

Common Patterns

常见模式

Basic Session

基础会话

swift
let model = SystemLanguageModel(useCase: .general)
let session = LanguageModelSession(model: model)
let response = try await session.respond(to: Prompt("Your question"))
swift
let model = SystemLanguageModel(useCase: .general)
let session = LanguageModelSession(model: model)
let response = try await session.respond(to: Prompt("Your question"))

Guided Generation

引导生成

swift
struct Recipe: Generable {
    let title: String
    let ingredients: [String]
}

let recipe = try await session.respond(
    generating: Recipe.self,
    prompt: Prompt("Create a pasta recipe")
)
swift
struct Recipe: Generable {
    let title: String
    let ingredients: [String]
}

let recipe = try await session.respond(
    generating: Recipe.self,
    prompt: Prompt("Create a pasta recipe")
)

Tool Calling

工具调用

swift
struct WeatherTool: Tool {
    func call(arguments: String) async throws -> String {
        // Fetch weather data
    }
}

let session = LanguageModelSession(
    model: model,
    tools: [WeatherTool()]
)
swift
struct WeatherTool: Tool {
    func call(arguments: String) async throws -> String {
        // Fetch weather data
    }
}

let session = LanguageModelSession(
    model: model,
    tools: [WeatherTool()]
)

Reference Documentation

参考文档

For complete API details, see the categorized documentation in the
references/
directory.
如需完整的API细节,请查看
references/
目录下的分类文档。