Layoutプロトコル
sizeThatFitsとplaceSubviewsを実装します。
「Layoutプロトコル」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Beyond Stacks
When stacks and grids are not enough, the Layout protocol lets you write your own container that positions children exactly how you want. 🧩
Two Required Methods
Conform to Layout by implementing sizeThatFits and placeSubviews, the two steps every custom layout needs.
struct MyLayout: Layout {
func sizeThatFits(...) -> CGSize { ... }
func placeSubviews(...) { ... }
}Sizing Yourself
In sizeThatFits, you measure the children and return the total size your container wants to occupy.
func sizeThatFits(proposal: ProposedViewSize,
subviews: Subviews,
cache: inout ()) -> CGSize {
CGSize(width: 200, height: 100)
}The Proposal
The ProposedViewSize is the space the parent offers. You decide how to use or shrink within it.
let width = proposal.width ?? 0The Subviews Proxy
The subviews argument is a collection of proxies. Ask each one for its preferred size before placing it.
for view in subviews {
let size = view.sizeThatFits(.unspecified)
}Placing Children
In placeSubviews, you loop and call place on each subview with an exact point and an anchor.
func placeSubviews(in bounds: CGRect,
proposal: ProposedViewSize,
subviews: Subviews,
cache: inout ()) {
}The place Call
Each place call sets the origin, the anchor point, and the size proposal for that one child.
view.place(at: point,
anchor: .topLeading,
proposal: .unspecified)Walk a Cursor
A simple layout keeps an x cursor, placing each child then advancing by its width plus spacing.
var x = bounds.minX
for view in subviews {
let s = view.sizeThatFits(.unspecified)
view.place(at: CGPoint(x: x, y: bounds.minY),
proposal: .unspecified)
x += s.width
}Using Your Layout
A Layout type is used just like a stack: wrap your views in it and they get arranged by your code.
MyLayout {
Text("One")
Text("Two")
}Optional Cache
The cache parameter lets you store expensive measurements between passes; use Void when you do not need it.
It Is Value-Based
Because a Layout is a struct, SwiftUI can re-run it cheaply, keeping custom containers fast and predictable. ⚡
Quick Check
Recall the two methods every Layout must implement.
Recap
You can now build a custom container with the Layout protocol by measuring in sizeThatFits and positioning in placeSubviews. 🎉
よくある質問
「Layoutプロトコル」レッスンは無料ですか?
はい。「Layoutプロトコル」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「Layoutプロトコル」で何を学びますか?
sizeThatFitsとplaceSubviewsを実装します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Layoutプロトコル」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- GeometryReaderの基本
- カスタムアライメントガイド
- Layoutプロトコル
- フローレイアウトを作る