Auto Layout z NSLayoutConstraint
Buduj w kodzie adaptacyjne interfejsy UIKit za pomocą ograniczeń Auto Layout, aby widoki poprawnie zmieniały rozmiar na różnych urządzeniach i w różnych orientacjach.
Auto Layout z NSLayoutConstraint to bezpłatna lekcja Objective-C iOS Development for Legacy & Enterprise Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Objective-C iOS Development for Legacy & Enterprise Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Objective-C iOS Development for Legacy & Enterprise Apps zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Auto Layout z NSLayoutConstraint” jest bezpłatna?
Tak — pełny tekst „Auto Layout z NSLayoutConstraint” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Objective-C iOS Development for Legacy & Enterprise Apps, przejdź na CoddyKit PRO. Kurs Objective-C iOS Development for Legacy & Enterprise Apps zawiera 4 lekcji w sumie.
Co nauczysz się w „Auto Layout z NSLayoutConstraint”?
Buduj w kodzie adaptacyjne interfejsy UIKit za pomocą ograniczeń Auto Layout, aby widoki poprawnie zmieniały rozmiar na różnych urządzeniach i w różnych orientacjach. Ćwiczysz Objective-C iOS Development for Legacy & Enterprise Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Objective-C iOS Development for Legacy & Enterprise Apps?
Nie wymagamy żadnego doświadczenia. Objective-C iOS Development for Legacy & Enterprise Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Auto Layout z NSLayoutConstraint”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Objective-C iOS Development for Legacy & Enterprise Apps?
Tak. Każda lekcja Objective-C iOS Development for Legacy & Enterprise Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- UIView i UIViewController
- Typowe kontrolki interfejsu i zdarzenia
- Widoki tabel i widoki kolekcji
- Auto Layout z NSLayoutConstraint