Difference between viewDidLoad and viewDidAppear
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
viewDidLoad and viewDidAppear are both part of the UIKit view-controller lifecycle, but they solve different problems. viewDidLoad is for one-time setup after the view hierarchy is loaded into memory. viewDidAppear is for work that should happen each time the view becomes visible to the user.
When viewDidLoad Runs
viewDidLoad is called after the controller loads its view hierarchy into memory. For a typical controller, that happens once during the lifetime of the loaded view.
Use it for:
- Initial UI setup.
- Static data binding.
- Registering cells and delegates.
- Starting one-time non-visual configuration.
Example:
This is the right place for setup that does not depend on the view already being on screen.
When viewDidAppear Runs
viewDidAppear runs after the view is actually visible. It can run many times during the lifetime of the controller.
Use it for:
- Starting animations.
- Refreshing visible data each time the screen returns.
- Triggering analytics for screen display.
- Starting work that depends on being onscreen.
Example:
If the user navigates away and back, viewDidAppear runs again.
Common Rule of Thumb
A practical rule is:
- If it should happen once per loaded view, use
viewDidLoad. - If it should happen every time the screen becomes visible, use
viewDidAppear.
That rule is not perfect, but it prevents most misuse.
Data Loading Tradeoffs
Many developers ask where network loading belongs. The answer depends on desired behavior.
If data should be fetched once when the view is first created:
If data should refresh whenever the user returns to the screen:
Putting refresh logic in viewDidLoad can leave stale data visible after navigation back from another screen.
Layout and Size-Dependent Work
Some work belongs in neither method. If you need final view sizes, viewDidLayoutSubviews is often a better fit because layout is not guaranteed to be final in viewDidLoad.
That matters for:
- Gradient layers sized to the view.
- Geometry-dependent drawing.
- Layout that depends on safe-area values after initial placement.
Do not force all screen setup into viewDidLoad just because it is early.
Animation and User-Visible Behavior
Animations often belong in viewDidAppear, not viewDidLoad, because the view must already be on screen for the user to see them.
If that code runs in viewDidLoad, the animation may finish before the user ever sees the screen.
Analytics and Lifecycle Intent
Screen-view analytics typically belong in viewDidAppear because analytics usually care about actual visibility, not just view creation.
Similarly, expensive one-time wiring such as delegate setup or initial label configuration belongs in viewDidLoad.
Keeping those responsibilities separate makes lifecycle code easier to reason about.
Common Pitfalls
- Putting one-time setup in
viewDidAppearand repeating it unnecessarily. - Starting user-visible animations in
viewDidLoad. - Refreshing returning-screen data only in
viewDidLoadand showing stale content. - Using
viewDidLoadfor size-dependent layout that should wait for layout callbacks. - Forgetting that
viewDidAppearmay run many times.
Summary
- '
viewDidLoadis for one-time setup after the view is loaded.' - '
viewDidAppearis for work that should happen whenever the view becomes visible.' - Data loading belongs in one or the other depending on whether refresh is needed.
- Animations and visibility analytics usually belong in
viewDidAppear. - Size-dependent UI work may belong in later layout callbacks instead of either method.

