iOS Development
Swift Programming
UIKit
Mobile App Development
iOS Lifecycle

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:

swift
1final class ProfileViewController: UIViewController {
2    @IBOutlet private weak var titleLabel: UILabel!
3
4    override func viewDidLoad() {
5        super.viewDidLoad()
6        titleLabel.text = "Profile"
7        view.backgroundColor = .systemBackground
8    }
9}

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:

swift
1final class DashboardViewController: UIViewController {
2    override func viewDidAppear(_ animated: Bool) {
3        super.viewDidAppear(animated)
4        print("Dashboard is now visible")
5    }
6}

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:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    loadInitialProfile()
4}

If data should refresh whenever the user returns to the screen:

swift
1override func viewDidAppear(_ animated: Bool) {
2    super.viewDidAppear(animated)
3    refreshBalance()
4}

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.

swift
1override func viewDidAppear(_ animated: Bool) {
2    super.viewDidAppear(animated)
3    cardView.alpha = 0
4    UIView.animate(withDuration: 0.3) {
5        self.cardView.alpha = 1
6    }
7}

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 viewDidAppear and repeating it unnecessarily.
  • Starting user-visible animations in viewDidLoad.
  • Refreshing returning-screen data only in viewDidLoad and showing stale content.
  • Using viewDidLoad for size-dependent layout that should wait for layout callbacks.
  • Forgetting that viewDidAppear may run many times.

Summary

  • 'viewDidLoad is for one-time setup after the view is loaded.'
  • 'viewDidAppear is 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.

Course illustration
Course illustration

All Rights Reserved.