การเพิ่มระยะขอบรอบ View
ใช้ระยะขอบเพื่อสร้างพื้นที่ว่างโดยรอบ
การเพิ่มระยะขอบรอบ View เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Views Feel Cramped
By default a SwiftUI view sits flush against its neighbors and the screen edge. Padding is the breathing room you add so content never feels squeezed.
Meet the padding Modifier
The padding modifier adds space on the outside of a view. Attach it like any other modifier and SwiftUI grows the view's footprint to make room.
Text("Hello")
.padding()The Default Amount
Calling padding() with no value uses a system default that adapts to the platform, usually around 16 points. It is a safe, consistent starting point.
Setting an Exact Amount
Pass a number to control the gap precisely. Here padding(40) adds 40 points of space on every side of the text.
Text("Spaced")
.padding(40)Padding One Edge
You can target a single edge instead of all four. This adds 30 points only on top, leaving the other sides untouched.
Text("Top gap")
.padding(.top, 30)Horizontal and Vertical
The .horizontal and .vertical edge sets are handy shortcuts. This pads the left and right sides at once with one clean call.
Text("Sides")
.padding(.horizontal, 24)Combining Edge Sets
Stack two padding calls to mix amounts per direction. Here the view gets wide horizontal margins and a smaller vertical gap.
Text("Mixed")
.padding(.horizontal, 32)
.padding(.vertical, 8)Order Matters
Modifiers apply outward in order, so where you place padding changes the result. Padding before a background pushes the color outward with the content.
Text("Hi")
.padding()
.background(.yellow)Padding Then Background
Swap the order and the difference is clear: a background applied first, then padding, leaves the color tight to the text with empty space around it.
Text("Hi")
.background(.yellow)
.padding()Padding on Containers
Padding works on stacks and any view, not just text. Wrap a VStack and pad it to push the whole group inward from the screen edges.
VStack {
Text("Line one")
Text("Line two")
}
.padding()Padding vs Margins
SwiftUI has no separate margin concept. Padding is your single tool for spacing, and chaining it gives you full control over every edge.
Quick Check
Test your grip on the padding modifier.
Recap
Nice work! You learned that padding adds breathing room outside a view, with defaults, exact amounts, specific edges, and an order that shapes backgrounds.
คำถามที่พบบ่อย
บทเรียน “การเพิ่มระยะขอบรอบ View” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเพิ่มระยะขอบรอบ View” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเพิ่มระยะขอบรอบ View”
ใช้ระยะขอบเพื่อสร้างพื้นที่ว่างโดยรอบ คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การเพิ่มระยะขอบรอบ View” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเพิ่มระยะขอบรอบ View
- การกำหนดขนาด View ด้วย frame
- พื้นหลังและรัศมีมุม
- Spacer และลำดับความสำคัญของเค้าโครง