ViewBuilder 与泛型容器
使用 @ViewBuilder 接受任意内容。
ViewBuilder 与泛型容器 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 SwiftUI Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 SwiftUI Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Containers That Hold Anything
VStack and List accept whatever views you nest inside them. You can build your own container that holds arbitrary content too. 📦
What @ViewBuilder Does
The @ViewBuilder attribute lets a closure list several views without commas or arrays. It is the magic behind stacks.
VStack {
Text("One")
Text("Two")
}Accept content as a Closure
Make your container take a content closure marked @ViewBuilder. Callers then pass views just like they do for VStack.
struct Card<Content: View>: View {
@ViewBuilder var content: Content
}Generic over Content
The container is generic over its content type. That single struct can wrap a Text, an image, or a whole layout.
Render the Content
In your body, place the stored content inside whatever wrapper you want, like padding and a background.
var body: some View {
content.padding().background(.gray)
}Use Your Container
Call it with a trailing closure and nest any views inside. Your custom wrapper now feels just like a built-in one.
Card {
Text("Title")
Text("Body")
}Builder on Functions Too
Mark a helper function with @ViewBuilder to return different views from branches without wrapping them in a type.
@ViewBuilder
func footer(show: Bool) -> some View {
if show { Text("More") } else { EmptyView() }
}Conditionals in Builders
Inside a builder you can use if and switch freely. SwiftUI assembles the chosen branch into the final view tree.
Multiple Content Slots
A container can take more than one builder, like a header and a footer. Each slot is its own @ViewBuilder closure.
struct Panel<H: View, B: View>: View {
@ViewBuilder var header: H
@ViewBuilder var body2: B
}Reusable Layout Shells
Generic containers capture a layout shell once, like a bordered card, and let every screen fill it with unique content.
Type-Safe and Flexible
Because content is generic, the compiler still checks every view. You get flexibility without losing type safety.
Quick Check
Quick check on generic containers.
Recap: Build Flexible Containers
Combine @ViewBuilder with a generic content type to make containers that wrap any views. Reusable shells, full type safety. 🧠
用 AI 导师学习 Swift — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 30
- 课程
- 120
常见问题解答
「ViewBuilder 与泛型容器」课时是免费的吗?
是的 — 「ViewBuilder 与泛型容器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。
「ViewBuilder 与泛型容器」这节课中我会学到什么?
使用 @ViewBuilder 接受任意内容。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 SwiftUI Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「ViewBuilder 与泛型容器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 SwiftUI Academy 课中编写并运行代码吗?
能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 提取可复用视图
- 自定义 ViewModifiers
- ButtonStyle 与 LabelStyle
- ViewBuilder 与泛型容器