أدلة المحاذاة المخصصة
حاذِ الواجهات بدقة عبر التسلسل الهرمي
أدلة المحاذاة المخصصة درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. 🎉
الأسئلة الشائعة
هل درس «أدلة المحاذاة المخصصة» مجاني؟
نعم — نص درس «أدلة المحاذاة المخصصة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «أدلة المحاذاة المخصصة»؟
حاذِ الواجهات بدقة عبر التسلسل الهرمي تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «أدلة المحاذاة المخصصة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أساسيات GeometryReader
- أدلة المحاذاة المخصصة
- بروتوكول Layout
- بناء تخطيط Flow