skip to content

In UIKit, why is viewIsAppearing(_:) better than viewWillAppear(_:) for work that needs the view's size or traits, and which iOS releases can call it?

level: middleimportance: should knowfreq 55%

answer

  1. same moment on screen, different state
  2. the view is not yet in a window
  3. traits, size and safe area become valid
  4. the transition coordinator belongs to the earlier one
  5. iOS 17 SDK, deploys back to iOS 13

basics

~20 s

viewWillAppear(:) runs before the view joins the hierarchy, so traits, size and safe area may be stale; viewIsAppearing(:) runs just after, once per appearance, with all of them valid. It shipped in the iOS 17 SDK and back-deploys to iOS 13.

solid answer

~40 s

`viewWillAppear(_:)` runs before UIKit adds the view to the hierarchy, so the view has no window yet and its trait collection, bounds, safe area insets and layout margins can still be the old values. `viewIsAppearing(_:)` runs shortly afterwards, once per appearance, after the view is added and laid out by its superview, so all of those are accurate. Both run in the same Core Animation transaction, so the user sees changes from either at the same moment. Apple's documentation makes `viewIsAppearing(_:)` the default for updating views; `viewWillAppear(_:)` stays for alongside animations on the `transitionCoordinator` and for balanced registrations that do not depend on traits. It arrived with the iOS 17 SDK and is declared available from iOS 13.

code

swift · 25 lines
swift
import UIKit

final class ParticipantListViewController: UIViewController {
    private let collectionView = UICollectionView(
        frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    var speakerIndexPath: IndexPath?

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // The coordinator exists here and not in viewIsAppearing(_:).
        transitionCoordinator?.animate(alongsideTransition: { _ in
            self.navigationController?.navigationBar.alpha = 0.6
        }, completion: nil)
    }

    override func viewIsAppearing(_ animated: Bool) {
        super.viewIsAppearing(animated)
        // Size, safe area and traits are final, so the scroll lands correctly.
        let compact = traitCollection.horizontalSizeClass == .compact
        collectionView.contentInset.bottom = compact ? 72 : 0
        if let speakerIndexPath {
            collectionView.scrollToItem(at: speakerIndexPath, at: .centeredVertically, animated: false)
        }
    }
}

go deeper

for a junior

Know that viewIsAppearing(:) exists, sits between viewWillAppear(:) and viewDidAppear(_:), and is where size and trait reads are safe.

for a middle

Explain what is not yet true in viewWillAppear(:), why the two callbacks look simultaneous to the user, and the two cases where viewWillAppear(:) remains the right choice.

for a senior

Diagnose wrong-offset scrolls and zero safe-area insets as early reads, and replace dispatch-async workarounds with viewIsAppearing(_:) without breaking alongside animations.

for a principal

Decide whether a codebase migrates its appearance code to viewIsAppearing(_:) wholesale or case by case, given it back-deploys to every supported release.

## The gap between two callbacks When a view controller is about to appear, UIKit calls `viewWillAppear(_:)`, then adds the view controller's view to the view hierarchy and lets its superview lay it out, then calls `viewIsAppearing(_:)`. Both happen at the start of the appearance transition and inside the same **Core Animation transaction** (`CATransaction`), so anything changed in either becomes visible at the same time. What differs is the state of the world when your code runs. | State at callback time | `viewWillAppear(_:)` | `viewIsAppearing(_:)` | |---|---|---| | Transition coordinator available for alongside animations | yes | no | | View added to the hierarchy and has a window | no | yes | | View controller and view trait collections updated | no | yes | | Geometry (bounds, safe area insets, layout margins) accurate | no | yes | A **trait collection** (`UITraitCollection`) is the set of environment values a view inherits: size classes, light or dark appearance, Dynamic Type size, display scale and more. A view outside a window can carry stale traits; once it is in the hierarchy it inherits current ones. ## Symptoms of using the earlier callback Code that reads geometry or traits in `viewWillAppear(_:)` works most of the time and then fails in exactly the cases interviewers ask about: - Scrolling a collection view to the active speaker's tile lands at the wrong offset, because the collection view does not have its final size yet. - Insets computed from `view.safeAreaInsets` are zero on the first appearance. - A layout chosen from `traitCollection.horizontalSizeClass` can be wrong when the screen is shown in a narrow iPad window, because the size class may still be the previous one. Moving the same code to `viewIsAppearing(_:)` fixes all three without any dispatch to the next run loop turn, which was the usual workaround before this callback existed. ## When viewWillAppear(_:) is still right Apple's documentation names two cases: 1. **Alongside animations.** The `transitionCoordinator` can take animations to run with the transition only before the transition starts, so `animate(alongsideTransition:completion:)` belongs in `viewWillAppear(_:)`. 2. **Balanced callbacks that ignore traits and geometry.** Registering for a data change notification in `viewWillAppear(_:)` and unregistering in `viewDidDisappear(_:)` does not care about the hierarchy. For everything else, the documentation calls `viewIsAppearing(_:)` a direct replacement to choose by default. ## Not a layout callback, not viewDidAppear(_:) - `viewWillLayoutSubviews()` and `viewDidLayoutSubviews()` run whenever the view lays out, which can be many times during one transition and at any time while the view is visible. `viewIsAppearing(_:)` runs **once per appearance**, and runs even if the view needs no layout. A one-shot action such as scrolling to an item belongs in `viewIsAppearing(_:)`; putting it in a layout callback repeats it and fights the user's own scrolling. - `viewDidAppear(_:)` runs after the transition animation has finished. Updating views there means the user watches the screen arrive in the wrong state and then jump. ## Reviewing existing appearance code When a codebase predates the iOS 17 SDK, its `viewWillAppear(_:)` overrides are worth a pass. Signs that a line belongs in `viewIsAppearing(_:)` instead: - it reads `view.bounds`, `view.frame`, `view.safeAreaInsets` or `view.layoutMargins`; - it reads `traitCollection`, for example to pick a layout by size class or to resolve a dynamic color; - it walks `view.window` or the superview chain; - it is wrapped in `DispatchQueue.main.async` with a comment about the view not being ready. Lines that should stay where they are: - calls on `transitionCoordinator`, such as alongside animations for a navigation bar fade; - start-and-stop pairs that do not touch the hierarchy, such as observing a model change notification that `viewDidDisappear(_:)` removes again. Moving code is low risk because the two callbacks run back to back in one transaction: the user sees no difference in timing, only the correct values. ## Availability `viewIsAppearing(_:)` arrived with the **iOS 17 SDK** and is declared `API_AVAILABLE(ios(13.0))`: an app built with that SDK or a later one gets the call on devices back to iOS 13. Every device in a typical iOS 17 or 18 deployment baseline therefore calls it, and no availability check is needed. Like the other appearance callbacks, an override calls `super`.

  • In UIKit, if both viewWillAppear(_:) and viewIsAppearing(_:) change a label, can the user see the first value flash?
    No. Both callbacks run inside the same Core Animation transaction at the start of the appearance transition, so only the final state is committed and drawn. That is why moving work from `viewWillAppear(_:)` to `viewIsAppearing(_:)` changes what values the code reads, not what the user sees or when.
  • In UIKit, why was dispatching to the main queue from viewWillAppear(_:) a poor fix for wrong geometry?
    Deferring the work to the next run loop turn usually lets the view finish joining the window and being laid out, but nothing guarantees it, and the change can land after the first frame is drawn, so the user may see a jump. `viewIsAppearing(_:)` gives a defined point where traits and geometry are valid before anything is drawn.

  • viewIsAppearing(_:) needs an iOS 17 deployment target to be called
  • viewIsAppearing(_:) runs after the appearance animation finishes
  • Changes made in viewWillAppear(_:) appear on screen a frame earlier
  • A one-time scroll to an item belongs in viewDidLayoutSubviews()
  • viewIsAppearing(_:) is where you add alongside animations to the transition coordinator

Assumes the iOS 27.1 SDK; viewIsAppearing(_:) shipped with the iOS 17 SDK and is declared available from iOS 13, so every iOS 17 baseline device calls it.