Auto Layout dengan NSLayoutConstraint
Bangun antarmuka UIKit adaptif dalam kode menggunakan batasan Auto Layout agar tampilan berubah ukuran dengan benar di berbagai perangkat dan orientasi.
Auto Layout dengan NSLayoutConstraint adalah pelajaran Objective-C iOS Development for Legacy & Enterprise Apps gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Objective-C iOS Development for Legacy & Enterprise Apps, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Objective-C iOS Development for Legacy & Enterprise Apps mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Auto Layout dengan NSLayoutConstraint” gratis?
Ya — teks lengkap “Auto Layout dengan NSLayoutConstraint” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Objective-C iOS Development for Legacy & Enterprise Apps, upgrade ke CoddyKit PRO. Kursus Objective-C iOS Development for Legacy & Enterprise Apps mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Auto Layout dengan NSLayoutConstraint”?
Bangun antarmuka UIKit adaptif dalam kode menggunakan batasan Auto Layout agar tampilan berubah ukuran dengan benar di berbagai perangkat dan orientasi. Kamu berlatih Objective-C iOS Development for Legacy & Enterprise Apps dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Objective-C iOS Development for Legacy & Enterprise Apps?
Tidak diperlukan pengalaman sebelumnya. Objective-C iOS Development for Legacy & Enterprise Apps di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Auto Layout dengan NSLayoutConstraint” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Objective-C iOS Development for Legacy & Enterprise Apps ini?
Ya. Setiap pelajaran Objective-C iOS Development for Legacy & Enterprise Apps menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- UIView dan UIViewController
- Kontrol dan Peristiwa UI Umum
- Tampilan Tabel dan Tampilan Koleksi
- Auto Layout dengan NSLayoutConstraint