Protokół Layout
Zaimplementuj sizeThatFits i placeSubviews
Protokół Layout to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🎉
Często zadawane pytania
Czy lekcja „Protokół Layout” jest bezpłatna?
Tak — pełny tekst „Protokół Layout” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Protokół Layout”?
Zaimplementuj sizeThatFits i placeSubviews Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?
Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Protokół Layout”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?
Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.