التخطيط التلقائي باستخدام NSLayoutConstraint
أنشئ واجهات UIKit متكيفة برمجيًا باستخدام قيود Auto Layout، بحيث تغيّر طرق العرض حجمها بشكل صحيح عبر الأجهزة والاتجاهات المختلفة.
التخطيط التلقائي باستخدام NSLayoutConstraint درس مجاني في Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Objective-C iOS Development for Legacy & Enterprise Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Auto Layout?
Hardcoded frames break on different screen sizes and rotations. Auto Layout describes relationships between views — constraints — and the system computes the right frames at runtime.
Constraints Describe Rules
A constraint is a linear equation: one attribute equals a multiple of another plus a constant. For example, a button's leading edge equals its superview's leading edge plus 20 points.
Disabling Autoresizing Masks
When adding constraints in code, you must first turn off the old autoresizing system on the view, or constraints will conflict.
label.translatesAutoresizingMaskIntoConstraints = NO;Creating a Constraint
The classic API is NSLayoutConstraint constraintWithItem:. It is verbose but explicit.
NSLayoutConstraint *c =
[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0 constant:20];Activating Constraints
A constraint does nothing until activated. Use activateConstraints: to turn an array of them on at once.
[NSLayoutConstraint activateConstraints:@[c]];Anchor API
Modern UIKit offers a cleaner anchor syntax. Each view exposes anchors like leadingAnchor and topAnchor.
[label.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:20].active = YES;
[label.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:40].active = YES;Width and Height
Constrain size directly with dimension anchors.
[label.widthAnchor constraintEqualToConstant:200].active = YES;
[label.heightAnchor constraintEqualToConstant:44].active = YES;Safe Area Layout Guide
Pin views to the safeAreaLayoutGuide so content stays clear of notches, status bars, and home indicators.
[label.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor].active = YES;Constraint Priorities
Every constraint has a priority from 1 to 1000. When constraints conflict, lower-priority ones yield. Required constraints are 1000; flexible ones use values like 750.
c.priority = UILayoutPriorityDefaultHigh; // 750Intrinsic Content Size
Views like labels and buttons report an intrinsic content size based on their text. You often only need to pin position, letting the view size itself.
Debugging Layout
Conflicts print warnings to the console listing the fighting constraints. Use the View Hierarchy Debugger in Xcode to inspect frames and constraints visually.
Quick Check
Test your Auto Layout knowledge.
Recap
You learned Auto Layout in UIKit:
- Constraints express relationships, not fixed frames
- Set
translatesAutoresizingMaskIntoConstraints = NO - Create with
NSLayoutConstraintor the cleaner anchor API - Activate constraints and pin to the safe area
- Use priorities and intrinsic size for flexible layouts
Auto Layout makes legacy UIs adapt to every device.
الأسئلة الشائعة
هل درس «التخطيط التلقائي باستخدام NSLayoutConstraint» مجاني؟
نعم — نص درس «التخطيط التلقائي باستخدام NSLayoutConstraint» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Objective-C iOS Development for Legacy & Enterprise Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.
ماذا ستتعلم في «التخطيط التلقائي باستخدام NSLayoutConstraint»؟
أنشئ واجهات UIKit متكيفة برمجيًا باستخدام قيود Auto Layout، بحيث تغيّر طرق العرض حجمها بشكل صحيح عبر الأجهزة والاتجاهات المختلفة. تتمرن على Objective-C iOS Development for Legacy & Enterprise Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Objective-C iOS Development for Legacy & Enterprise Apps؟
لا تُشترط خبرة سابقة. Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التخطيط التلقائي باستخدام NSLayoutConstraint»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Objective-C iOS Development for Legacy & Enterprise Apps هذا؟
نعم. كل درس في Objective-C iOS Development for Legacy & Enterprise Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- UIView وUIViewController
- عناصر تحكم UI والأحداث الشائعة
- عروض الجداول وعروض المجموعات
- التخطيط التلقائي باستخدام NSLayoutConstraint