skip to content

In UIKit, a call screen embeds participant tiles as child view controllers, but each tile's didMove(toParent:) setup never runs and removed tiles stay in memory; what is wrong?

level: seniorimportance: should knowfreq 45%

answer

  1. containment is a three-step handshake
  2. one notification is automatic, one is yours
  3. removing the view is not removing the child
  4. children keeps the child alive
  5. willMove(toParent: nil) before removeFromParent()

basics

~20 s

addChild(_:) calls the child's willMove(toParent:) but not didMove(toParent:), which the container must call after adding the child's view. Removing only the view leaves the child in children, still alive; removal needs willMove(toParent: nil), removing the view, then removeFromParent().

solid answer

~40 s

View controller containment is a handshake with fixed steps. Adding: `addChild(_:)` makes the parent-child link and calls the child's `willMove(toParent:)` for you; you add the child's view to your hierarchy; then you call `child.didMove(toParent: self)` yourself, after any transition. A container that skips that last call leaves every tile's `didMove(toParent:)` override silent. Removing runs in reverse: call `child.willMove(toParent: nil)`, remove its view from its superview, then call `removeFromParent()`, which calls `didMove(toParent: nil)` for you. A container that only calls `removeFromSuperview()` leaves the tile in its `children` array, so the parent keeps it alive and keeps forwarding size changes to it. A child's `parent` is weak, so the ownership runs from parent to child.

code

swift · 17 lines
swift
import UIKit

extension UIViewController {
    func embed(_ child: UIViewController, in container: UIView) {
        addChild(child)                       // calls child.willMove(toParent: self)
        child.view.frame = container.bounds
        child.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        container.addSubview(child.view)
        child.didMove(toParent: self)         // yours to call, after the view is in place
    }

    func unembed(_ child: UIViewController) {
        child.willMove(toParent: nil)         // yours to call before removing
        child.view.removeFromSuperview()
        child.removeFromParent()              // calls child.didMove(toParent: nil)
    }
}

go deeper

for a junior

Know that embedding another view controller's view needs addChild(_:), adding the view, and didMove(toParent:), in that order.

for a middle

Explain which containment notification each UIKit method sends automatically and which the container must send, for both adding and removing a child.

for a senior

Diagnose silent didMove(toParent:) overrides and growing children arrays from the symptoms, and tell a containment leak from a closure capture with the memory graph.

for a principal

Decide when a tile deserves its own child view controller versus a plain view, weighing forwarded callbacks against the handshake every container must get right.

## What containment is for A **container view controller** shows the views of other view controllers inside its own view. `UINavigationController` and `UITabBarController` are containers; so is a call screen that gives each participant tile its own view controller to manage its video, name label and mute badge. The parent-child link matters because UIKit uses it to route messages: - **Appearance callbacks** reach a child because its parent forwards them; `shouldAutomaticallyForwardAppearanceMethods` is `true` by default. - **Size changes** through `viewWillTransition(to:with:)` are forwarded from a view controller to its child view controllers. - `parent`, `navigationController` and related properties on the child are only set when the link exists. Adding a child's view with `addSubview(_:)` alone, without `addChild(_:)`, produces a tile that looks right but sits outside all of this. ## The adding handshake `addChild(_:)` creates the relationship. If the child already has a different parent, it is first removed from it. The documentation and the header are precise about which notification each step sends: 1. `parent.addChild(child)` calls `child.willMove(toParent: parent)` **automatically**. 2. The container adds `child.view` to its own view hierarchy and sizes it, with constraints or a frame. 3. The container calls `child.didMove(toParent: parent)` **itself**, after the transition finishes, or immediately when there is no transition. UIKit does not make the third call for you. A tile that starts its video renderer, or registers with the call's audio level meter, in an override of `didMove(toParent:)` never does so when the container forgets step 3. That is the first symptom in the question. ## The removing handshake Removal mirrors the add, with the automatic and manual calls swapped: 1. The container calls `child.willMove(toParent: nil)` **itself**; `removeFromParent()` does not send it. 2. The container removes the child's view with `child.view.removeFromSuperview()`. 3. The container calls `child.removeFromParent()`, which calls `child.didMove(toParent: nil)` **automatically**. | Step | Adding | Removing | |---|---|---| | `willMove(toParent:)` | sent by `addChild(_:)` | you call it, with `nil` | | View | you add `child.view` | you remove `child.view` | | `didMove(toParent:)` | you call it | sent by `removeFromParent()` | When a participant leaves and the container only removes the tile's view, the tile view controller is still in the parent's `children` array. The parent holds its children, while a child's `parent` property is weak, so the tile stays alive for as long as the call screen does. It also keeps receiving forwarded size changes on every rotation, and its `deinit` never runs. That is the second symptom: memory that grows with each participant who leaves, caused by containment, not by a closure capture. ## Custom appearance forwarding A container that needs to control timing itself, for example to delay a tile's appearance until its video arrives, overrides `shouldAutomaticallyForwardAppearanceMethods` to return `false`. It must then drive the child's appearance callbacks with `beginAppearanceTransition(_:animated:)` and `endAppearanceTransition()`, not by calling `viewWillAppear(_:)` and friends directly; the begin/end pair makes sure the child's own descendants are notified too. For swapping one child for another, such as replacing the grid with a single speaker view, `transition(from:to:duration:options:animations:completion:)` adds the new child's view and removes the old one for you. Both must already be children of the receiver, and the add and remove handshake calls still bracket it. ## What the child sees A child can tell how it is arriving or leaving from inside its own appearance callbacks: `isMovingToParent` is true in its appear callbacks when it is being added to a container, and `isMovingFromParent` is true in its disappear callbacks when it is being removed. These are the documented signals for one-time work tied to joining or leaving the call grid, as opposed to merely being covered by a presented screen. Note that `children` lists only contained view controllers; a screen the call presents modally is never in it. ## Diagnosing it - Count `children` after participants leave; a number that only grows means removal skipped `removeFromParent()`. - Put a breakpoint or log in a tile's `didMove(toParent:)`; if it never fires with the parent, step 3 of the add is missing. - Use the memory graph to confirm the tile is held from the parent's children, not from a capture.

  • In UIKit, what goes wrong if a call screen adds a tile's view with addSubview(_:) but never calls addChild(_:)?
    The tile view controller is not in the parent's `children`, so the parent does not forward appearance callbacks or size changes to it, and its `parent` and `navigationController` are nil. Its view still draws, which is why the bug hides until rotation, presentation or navigation from inside the tile misbehaves. Something else must also keep that view controller alive.
  • In UIKit, when should a container return false from shouldAutomaticallyForwardAppearanceMethods?
    When it must decide itself when a child counts as appearing, for example a carousel where a page should only appear once it settles. It then calls `beginAppearanceTransition(_:animated:)` and `endAppearanceTransition()` on each child at the right moments. Most containers keep the default `true` and let UIKit forward the callbacks.

  • addChild(_:) calls didMove(toParent:) on the child for you
  • Removing the child's view also removes the child view controller
  • A container should call the child's viewWillAppear(_:) directly
  • removeFromParent() sends willMove(toParent: nil) first
  • A leaked child tile must be a closure retain cycle