swiftui-programming-skill
Expertise in SwiftUI for building declarative user interfaces, including SF Symbols integration and best practices.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
SwiftUI Programming Skill
This skill provides comprehensive guidance on developing user interfaces using SwiftUI, Apple's modern framework for building apps across all Apple platforms. It emphasizes declarative programming, state management, and integration with system features like SF Symbols.
Best Practices
-
Declarative Syntax: Use SwiftUI's declarative approach to describe what the UI should look like, not how to build it.
-
State Management: Leverage
@State,@ObservedObject,@EnvironmentObjectfor managing view state. -
Composition: Build complex views by composing simpler views.
-
Accessibility: Always consider accessibility features from the start.
-
Performance: Use appropriate modifiers and avoid unnecessary view updates.
-
SF Symbols: Utilize Apple's symbol library for consistent iconography.
SwiftUI Guidelines
- Prefer built-in components over custom implementations when possible.
- Use SwiftUI's layout system (HStack, VStack, ZStack) effectively.
- Implement dark mode support automatically with Color and system colors.
- Handle orientation changes gracefully.
- Use
@AppStoragefor simple user preferences.
Examples
Example 1: Basic View with Toolbar
User Prompt: "Create a SwiftUI view with a navigation bar and toolbar items using SF Symbols."
Expected Output:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Text("Hello, World!")
.navigationTitle("My App")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
// Action for leading button
}) {
Image(systemName: "line.horizontal.3")
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
// Action for trailing button
}) {
Image(systemName: "plus")
}
}
}
}
}
}
Example 2: State Management with @State
User Prompt: "Show how to create a toggle button that changes the background color using SwiftUI."
Expected Output:
import SwiftUI
struct ToggleView: View {
@State private var isOn = false
var body: some View {
ZStack {
(isOn ? Color.blue : Color.gray)
.edgesIgnoringSafeArea(.all)
VStack {
Text(isOn ? "Light Mode" : "Dark Mode")
.foregroundColor(.white)
.font(.largeTitle)
Toggle("Toggle Mode", isOn: $isOn)
.padding()
.toggleStyle(SwitchToggleStyle(tint: .white))
}
}
}
}
Example 3: Using SF Symbols with Custom Styling
User Prompt: "How can I display an SF Symbol with custom colors and effects in SwiftUI?"
Expected Output:
import SwiftUI
struct SymbolView: View {
var body: some View {
VStack(spacing: 20) {
Image(systemName: "star.fill")
.font(.system(size: 50))
.foregroundColor(.yellow)
Image(systemName: "heart.fill")
.font(.system(size: 50))
.foregroundColor(.red)
.shadow(radius: 5)
Image(systemName: "moon.fill")
.font(.system(size: 50))
.foregroundColor(.blue)
.background(Circle().fill(Color.white).shadow(radius: 3))
.clipShape(Circle())
}
}
}
Example 4: List with Navigation
User Prompt: "Generate a SwiftUI list that navigates to detail views."
Expected Output:
import SwiftUI
struct Item: Identifiable {
let id = UUID()
let name: String
}
struct ListView: View {
let items = [Item(name: "Item 1"), Item(name: "Item 2"), Item(name: "Item 3")]
var body: some View {
NavigationView {
List(items) { item in
NavigationLink(destination: DetailView(item: item)) {
Text(item.name)
}
}
.navigationTitle("Items")
}
}
}
struct DetailView: View {
let item: Item
var body: some View {
Text("Detail for \(item.name)")
.navigationTitle(item.name)
}
}
Example 5: Form with Validation
User Prompt: "Create a SwiftUI form for user input with basic validation."
Expected Output:
import SwiftUI
struct FormView: View {
@State private var name = ""
@State private var email = ""
@State private var isValid = false
var body: some View {
NavigationView {
Form {
Section(header: Text("Personal Information")) {
TextField("Name", text: $name)
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.autocapitalization(.none)
}
Section {
Button("Submit") {
// Submit action
}
.disabled(!isValid)
}
}
.navigationTitle("User Form")
.onChange(of: name) { _ in validateForm() }
.onChange(of: email) { _ in validateForm() }
}
}
private func validateForm() {
isValid = !name.isEmpty && email.contains("@")
}
}
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-02-02
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!