0Pricing
SwiftUI Academy · レッスン

ViewBuilderとジェネリックコンテナ

@ViewBuilderで任意のコンテンツを受け取ります。

「ViewBuilderとジェネリックコンテナ」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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. 🧠

よくある質問

「ViewBuilderとジェネリックコンテナ」レッスンは無料ですか?

はい。「ViewBuilderとジェネリックコンテナ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「ViewBuilderとジェネリックコンテナ」で何を学びますか?

@ViewBuilderで任意のコンテンツを受け取ります。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「ViewBuilderとジェネリックコンテナ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 再利用可能なビューを切り出す
  2. カスタムViewModifier
  3. ButtonStyleとLabelStyle
  4. ViewBuilderとジェネリックコンテナ
← SwiftUI Academyに戻る