Objective-C iOS Development for Legacy & Enterprise Apps · レッスン

NSLayoutConstraintによるAuto Layout

Auto Layoutの制約をコードで使って、デバイスや向きが変わってもビューが正しくリサイズされる適応型UIKitインターフェースを構築します。

レッスン 4/413 ステップ

「NSLayoutConstraintによるAuto Layout」はCoddyKit上の無料Objective-C iOS Development for Legacy & Enterprise Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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; // 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.

無料で開始

AI チューターと学ぶ Objective-C — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「NSLayoutConstraintによるAuto Layout」レッスンは無料ですか?

はい。「NSLayoutConstraintによるAuto Layout」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Objective-C iOS Development for Legacy & Enterprise Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Objective-C iOS Development for Legacy & Enterprise Appsコースには全4レッスンが含まれています。

「NSLayoutConstraintによるAuto Layout」で何を学びますか?

Auto Layoutの制約をコードで使って、デバイスや向きが変わってもビューが正しくリサイズされる適応型UIKitインターフェースを構築します。 ブラウザで直接実行するハンズオンコードでObjective-C iOS Development for Legacy & Enterprise Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Objective-C iOS Development for Legacy & Enterprise Appsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのObjective-C iOS Development for Legacy & Enterprise Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「NSLayoutConstraintによるAuto Layout」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このObjective-C iOS Development for Legacy & Enterprise Appsレッスンでコードを書いて実行できますか?

はい。すべてのObjective-C iOS Development for Legacy & Enterprise Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. UIViewとUIViewController
  2. 一般的なUIコントロールとイベント
  3. テーブルビューとコレクションビュー
  4. NSLayoutConstraintによるAuto Layout
← Objective-C iOS Development for Legacy & Enterprise Appsに戻る