使用 frame 调整视图大小
设置宽度、高度以及最小和最大尺寸限制。
使用 frame 调整视图大小 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 SwiftUI Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 SwiftUI Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「使用 frame 调整视图大小」课时是免费的吗?
是的 — 「使用 frame 调整视图大小」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。
「使用 frame 调整视图大小」这节课中我会学到什么?
设置宽度、高度以及最小和最大尺寸限制。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 SwiftUI Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 frame 调整视图大小」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 SwiftUI Academy 课中编写并运行代码吗?
能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 为视图添加内边距
- 使用 frame 调整视图大小
- 背景与圆角
- Spacer 与布局优先级