Sizing Views with frame
Set width, height, and min/max constraints.
Sizing Views with frame is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Sizing Views with frame” lesson free?
Yes — the full text of “Sizing Views with frame” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Sizing Views with frame”?
Set width, height, and min/max constraints. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sizing Views with frame” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Adding Padding Around Views
- Sizing Views with frame
- Backgrounds & Corner Radius
- Spacer & Layout Priority