Sample CodeiOS, iPadOS, Mac CatalystReviewed 2026-07-21View on Apple Developer

Adjusting your layout with keyboard layout guide

At a glance

Item Summary
Purpose Repositions image and editing controls as a docked or floating keyboard approaches different screen edges.
App architecture One programmatic view controller creates the hierarchy and registers edge-conditioned constraints with UIKeyboardLayoutGuide.
Main patterns Adaptive constraint sets, local view factories, and view-controller composition.
Project style Minimal UIKit target: lifecycle boilerplate plus one feature controller.

Project structure

KeyboardTourGuide/
├── AppDelegate.swift
├── SceneDelegate.swift
├── ViewController.swift
└── Assets.xcassets/

Structure observations

  • All feature behavior is intentionally colocated because layout guide, created views, and constraints share one view hierarchy.
  • Helper methods separate hierarchy construction from adaptive constraint registration.

Overall architecture

Reference code

KeyboardTourGuide/ViewController.swift:11 — startup enables floating-keyboard tracking and registers the adaptive constraints once.

override func viewDidLoad() {
    super.viewDidLoad()
    view.keyboardLayoutGuide.followsUndockedKeyboard = true
    createTrackingConstraints()
}

Interpretation

The controller does not observe keyboard notifications or mutate frames. It declares mutually activated constraint groups, and UIKit selects them as the keyboard layout guide moves near or away from edges.

Ownership and state

Ownership evidence

KeyboardTourGuide/ViewController.swift:22 — the controller creates the feature views, inserts them, and binds their anchors to the guide.

func createTrackingConstraints() {
    let imageView = makeTitleAndImage()
    let editView = makeControlsView()
    view.addSubview(editView)

    let keyboardToImageView = view.keyboardLayoutGuide.topAnchor.constraint(
        greaterThanOrEqualToSystemSpacingBelow: imageView.bottomAnchor, multiplier: 1.0)
    view.keyboardLayoutGuide.setConstraints([keyboardToImageView], activeWhenNearEdge: .bottom)
}
Owner Object or state Relationship Mutation authority
View controller Feature hierarchy during construction Creates and adds Controller setup only
Root view / stack views Child views UIKit view hierarchy retains Hierarchy/layout system
Root view Keyboard layout guide UIKit-provided guide UIKit tracks keyboard geometry
Keyboard layout guide Registered constraint groups Manages activation by edge conditions UIKit

Class and protocol design

Type Responsibility Depends on
ViewController Build views and declare all keyboard-relative layout policies Auto Layout, keyboard/safe-area guides
UIKeyboardLayoutGuide Track keyboard frame and activate registered constraints UIKit keyboard lifecycle

No local protocols, model, service, delegate, or custom state owner are present. The architecture is deliberately a single layout example.

Access control

Symbol Access Verified effect Likely rationale
ViewController implicit internal Storyboard/target can instantiate it; it is not exported. App-only screen.
createTrackingConstraints, makeTitleAndImage, makeControlsView implicit internal Other target code could call construction helpers. Sample readability; current collaboration does not require this breadth.
Local views/constraints lexical locals Cannot be accessed after setup except through the retained hierarchy/guide. The implementation needs no ongoing imperative state.
No private / fileprivate members verified absence There is no explicit member-level boundary. Concise teaching sample rather than reusable API design.

Reference code

KeyboardTourGuide/ViewController.swift:101 — factory-created views are returned for composition; intermediate state stays local.

func makeTitleAndImage() -> UIImageView {
    let titleLabel = UILabel()
    let imageView = UIImageView(image: #imageLiteral(resourceName: "Plants_1_Succulents"))
    view.addSubview(titleLabel)
    view.addSubview(imageView)
    return imageView
}

Logic ownership and placement

Logic Owning type or file Placement rationale
Feature hierarchy creation ViewController helper methods Only this screen uses the controls.
Edge-conditioned policy createTrackingConstraints All related anchors and edge rules are visible together.
Keyboard geometry and constraint activation UIKit layout guide Framework already owns keyboard lifecycle.

Design patterns

Pattern Source evidence Purpose or tradeoff
Adaptive constraint set KeyboardTourGuide/ViewController.swift:39 Declaratively swaps layout rules based on keyboard proximity.
View factory helper KeyboardTourGuide/ViewController.swift:101 Keeps construction details separate without introducing extra types.
Framework-managed state KeyboardTourGuide/ViewController.swift:17 Avoids notification observers and stored keyboard-frame state.

Naming conventions

  • Methods use construction verbs: make... returns a view; createTrackingConstraints installs policy.
  • Constraint variables describe both endpoints or condition, such as editViewOnKeyboard and nearLeadingConstraints.
  • Constraint identifiers mirror variable names for debugging.

Architecture takeaways

  • Prefer keyboard-layout-guide constraints over imperative keyboard notification/frame handling.
  • Register near/away policies once and let UIKit own runtime activation.
  • Keep transient construction values local when the view hierarchy and layout guide retain everything needed.

Source map

Source file Relevant symbols
KeyboardTourGuide/ViewController.swift View factories and adaptive constraint groups
KeyboardTourGuide/SceneDelegate.swift Storyboard scene/window lifecycle
KeyboardTourGuide/AppDelegate.swift Application lifecycle boilerplate