แนวจัดตำแหน่งแบบกำหนดเอง
จัดแนวมุมมองข้ามลำดับชั้นอย่างแม่นยำ
แนวจัดตำแหน่งแบบกำหนดเอง เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Lining Things Up
Stacks align children using a shared edge or center. Alignment guides let you fine-tune exactly where that line falls. 📏
Built-in Alignments
An HStack takes a vertical alignment like .top or .center, while a VStack takes a horizontal one.
HStack(alignment: .top) {
Text("A")
Text("Big")
}The alignmentGuide Modifier
The alignmentGuide modifier overrides where a single view exposes a given alignment to its parent.
Text("Hi")
.alignmentGuide(.top) { d in d[.bottom] }Reading Dimensions
The closure gets a ViewDimensions value, letting you read d.width, d.height, or any named guide.
Text("Hi").alignmentGuide(.leading) { d in
d.width / 2
}Shift the Guide
Returning a different number moves the view relative to its peers, since the parent lines up everyone on that returned value.
Custom Alignment ID
For reuse, define a type conforming to AlignmentID with a defaultValue for unaligned cases.
struct MidBadge: AlignmentID {
static func defaultValue(in d: ViewDimensions) -> CGFloat {
d[.bottom]
}
}Name It on Alignment
Extend VerticalAlignment or HorizontalAlignment with a static property bound to your AlignmentID.
extension VerticalAlignment {
static let midBadge = VerticalAlignment(MidBadge.self)
}Use the Custom Line
Pass your named alignment to a stack, then tag the views that should snap to that custom line.
HStack(alignment: .midBadge) {
Avatar()
Label()
}Tag the Participants
Each view that should share the line sets alignmentGuide for your custom alignment to the point it wants to match.
Label()
.alignmentGuide(.midBadge) { d in
d[VerticalAlignment.center]
}Cross-Hierarchy Power
Custom guides shine when views live in different containers but must still align, like a caption to an icon center across columns. ✨
Mind the Default
Any view that does not set your custom guide falls back to its defaultValue, so pick that default thoughtfully.
Quick Check
Recall what the alignmentGuide closure receives.
Recap
You can now override alignment with alignmentGuide and define custom AlignmentID types to align views across the hierarchy. 🎉
คำถามที่พบบ่อย
บทเรียน “แนวจัดตำแหน่งแบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แนวจัดตำแหน่งแบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แนวจัดตำแหน่งแบบกำหนดเอง”
จัดแนวมุมมองข้ามลำดับชั้นอย่างแม่นยำ คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “แนวจัดตำแหน่งแบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐาน GeometryReader
- แนวจัดตำแหน่งแบบกำหนดเอง
- โพรโทคอลเลย์เอาต์
- สร้างเลย์เอาต์แบบไหล