Ustalanie rozmiaru widoków za pomocą frame
Proszę ustawiać szerokość, wysokość oraz ograniczenia minimalne i maksymalne.
Ustalanie rozmiaru widoków za pomocą frame to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 2 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.
Views Size Themselves
By default a SwiftUI view is exactly as big as its content needs. When you want a specific size, you reach for the frame modifier to ask for one.
A Fixed Width and Height
The frame modifier lets you request an exact size. Here the text is placed inside a box that is 200 points wide and 80 points tall.
Text("Card")
.frame(width: 200, height: 80)Width Only
You do not have to set both dimensions. Give only a width and the height stays natural, sized to fit the content as usual.
Text("Wide")
.frame(width: 250)Filling Available Space
Set maxWidth to infinity to let a view stretch across all the space its parent offers. This is great for full-width buttons and rows.
Text("Stretch")
.frame(maxWidth: .infinity)Aligning Inside the Frame
A roomy frame centers its content by default. The alignment argument moves that content, like pinning text to the leading edge.
Text("Left")
.frame(maxWidth: .infinity, alignment: .leading)Minimum Sizes
Use minWidth or minHeight to guarantee a view never shrinks below a floor, even when content is tiny. Handy for consistent tap targets.
Text("OK")
.frame(minWidth: 120)Maximum Sizes
The maxWidth and maxHeight values cap how large a flexible view can grow, so it stretches only up to a limit you choose.
Text("Capped")
.frame(maxWidth: 400)Combining Min and Max
Mix minWidth and maxWidth for a flexible range. The view grows with its parent but stays between a comfortable lower and upper bound.
Text("Flexible")
.frame(minWidth: 100, maxWidth: 300)Frame and Background Together
A frame defines the area a background fills. Set a frame, then add a color, and the whole sized region is painted, not just the text.
Text("Tile")
.frame(width: 120, height: 120)
.background(.mint)Frame Wraps, It Does Not Clip
A frame proposes a size but never trims content by itself. If content is larger than the frame, it can spill out unless you also clip it.
Frame Plus Padding
Combine the two tools for polished layouts: a frame sets the overall size and padding adds the inner breathing room around the content.
Text("Spaced")
.padding()
.frame(maxWidth: .infinity)Quick Check
Check your understanding of the frame modifier.
Recap
Great progress! You can now use frame to set exact sizes, flexible ranges, fill space with infinity, and align content inside the area.
Często zadawane pytania
Czy lekcja „Ustalanie rozmiaru widoków za pomocą frame” jest bezpłatna?
Tak — pełny tekst „Ustalanie rozmiaru widoków za pomocą frame” 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 „Ustalanie rozmiaru widoków za pomocą frame”?
Proszę ustawiać szerokość, wysokość oraz ograniczenia minimalne i maksymalne. Ć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 2 z 4.
Ile czasu zajmuje lekcja „Ustalanie rozmiaru widoków za pomocą frame”?
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.
Wszystkie lekcje w tym kursie
- Dodawanie marginesów wokół widoków
- Ustalanie rozmiaru widoków za pomocą frame
- Tła i promień zaokrąglenia
- Spacer i priorytet układu