UIView - How to get notified when the view is loaded?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UIView itself does not expose a single universal event named “view loaded,” so developers often need to pick the right lifecycle hook for the job. In practice, the correct callback depends on whether you are working in a UIViewController, a custom UIView, or a view loaded from a nib. Choosing the proper hook avoids duplicate work, layout bugs, and timing issues.
Understand the Lifecycle Boundaries
A common source of confusion is mixing controller lifecycle with view lifecycle. For controllers, viewDidLoad is called once after the root view is loaded into memory. For views, methods such as init, awakeFromNib, didMoveToSuperview, didMoveToWindow, and layoutSubviews are the key points.
Use this rule of thumb:
- Use
viewDidLoadfor one-time controller setup - Use
awakeFromNibfor nib-backed view setup - Use
didMoveToWindowwhen you need to know a view became visible in a window - Use
layoutSubviewsfor frame-dependent updates
Controller Example for One-Time Setup
If your question is really about screen initialization, start in UIViewController.
This callback runs once per controller instance, so it is safe for expensive setup.
Custom UIView Notification Pattern
For reusable custom views, create a small callback API that fires when the view is attached to a window. This pattern gives parent controllers a clear event without relying on guesswork.
Usage in a controller:
This is explicit and testable, and it avoids repeatedly firing logic during every layout pass.
Nib and Storyboard Cases
If the view is loaded from Interface Builder, awakeFromNib is typically the earliest safe point for outlet wiring and local initialization.
Avoid assumptions about frame size in awakeFromNib; geometry may still change later.
When You Need Final Geometry
If setup depends on final sizes, use layoutSubviews with an idempotent guard:
Without the guard, repeated layout passes can create duplicate layers and memory growth.
Practical Decision Matrix
Pick the callback based on intent:
- One-time screen setup:
viewDidLoad - View loaded from nib:
awakeFromNib - Need window attachment state:
didMoveToWindow - Need final frame:
layoutSubviews
This mental model reduces lifecycle mistakes in large UIKit codebases.
Common Pitfalls
- Running network requests from
layoutSubviews, which may execute many times - Expecting
viewDidLoadto run each time a screen appears - Doing frame-dependent calculations in
initorawakeFromNib - Forgetting guards in
didMoveToWindow, causing repeated side effects - Treating
UIViewandUIViewControllerlifecycles as interchangeable
Most bugs are caused by choosing a hook that does not match the desired lifecycle moment.
Summary
- There is no single
UIViewevent that universally means “fully loaded.” - Use controller callbacks for screen setup and view callbacks for reusable components.
didMoveToWindowis a solid signal when a view first becomes visible.- Reserve
layoutSubviewsfor geometry-sensitive work, guarded for idempotence. - Explicit callback patterns make lifecycle behavior easier to test and maintain.

