Auto Layout ด้วย NSLayoutConstraint
สร้างส่วนติดต่อผู้ใช้ UIKit ที่ปรับตามขนาดได้ด้วยข้อจำกัด Auto Layout ในโค้ด เพื่อให้มุมมองปรับขนาดได้ถูกต้องในอุปกรณ์และแนวหน้าจอต่าง ๆ
Auto Layout ด้วย NSLayoutConstraint เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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.
คำถามที่พบบ่อย
บทเรียน “Auto Layout ด้วย NSLayoutConstraint” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “Auto Layout ด้วย NSLayoutConstraint” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “Auto Layout ด้วย NSLayoutConstraint”
สร้างส่วนติดต่อผู้ใช้ UIKit ที่ปรับตามขนาดได้ด้วยข้อจำกัด Auto Layout ในโค้ด เพื่อให้มุมมองปรับขนาดได้ถูกต้องในอุปกรณ์และแนวหน้าจอต่าง ๆ คุณปฏิบัติ Objective-C iOS Development for Legacy & Enterprise Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Objective-C iOS Development for Legacy & Enterprise Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Objective-C iOS Development for Legacy & Enterprise Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “Auto Layout ด้วย NSLayoutConstraint” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps นี้ได้ไหม
ได้ บทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- UIView และ UIViewController
- ตัวควบคุมและเหตุการณ์ UI ทั่วไป
- มุมมองตารางและมุมมองคอลเลกชัน
- Auto Layout ด้วย NSLayoutConstraint