Objective-C iOS Development for Legacy & Enterprise Apps · Lezione

Auto Layout con NSLayoutConstraint

Costruisca interfacce UIKit adattive in codice usando i vincoli Auto Layout, così che le view si ridimensionino correttamente su dispositivi e orientamenti diversi.

Lezione 4 di 413 passaggi

Auto Layout con NSLayoutConstraint è una lezione Objective-C iOS Development for Legacy & Enterprise Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Objective-C iOS Development for Legacy & Enterprise Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Objective-C iOS Development for Legacy & Enterprise Apps include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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; // 750

Intrinsic 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 NSLayoutConstraint or 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.

Gratis per iniziare

Impara Objective-C con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Auto Layout con NSLayoutConstraint» è gratuita?

Sì — il testo completo di «Auto Layout con NSLayoutConstraint» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Objective-C iOS Development for Legacy & Enterprise Apps, passa a CoddyKit PRO. Il corso Objective-C iOS Development for Legacy & Enterprise Apps include 4 lezioni in totale.

Cosa imparerò in «Auto Layout con NSLayoutConstraint»?

Costruisca interfacce UIKit adattive in codice usando i vincoli Auto Layout, così che le view si ridimensionino correttamente su dispositivi e orientamenti diversi. Eserciti Objective-C iOS Development for Legacy & Enterprise Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Objective-C iOS Development for Legacy & Enterprise Apps?

Non è richiesta alcuna esperienza precedente. Objective-C iOS Development for Legacy & Enterprise Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Auto Layout con NSLayoutConstraint»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Objective-C iOS Development for Legacy & Enterprise Apps?

Sì. Ogni lezione Objective-C iOS Development for Legacy & Enterprise Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. UIView e UIViewController
  2. Controlli ed eventi comuni dell’interfaccia utente
  3. Viste tabella e viste collezione
  4. Auto Layout con NSLayoutConstraint
← Torna a Objective-C iOS Development for Legacy & Enterprise Apps