Simulated Network Loading - codepath/ios_guides GitHub Wiki
When creating mocks using Xcode, we can add an extra degree of fidelity by simulating a loading event when we transition to a new screen.

viewWillAppear and viewDidAppear methods
In the life cycle of a ViewController, the viewWillAppear method is called after the viewDidLoad method, but right before the view actually "appears". The moment the ViewController actually "appears", the viewDidAppear method is called.
By default, the viewWillAppear and viewDidAppear methods are not explicitly added to the Swift ViewController file. UIKit calls them automatically on the main thread for every view controller; override either method when you need to run custom code at that lifecycle point.
Unlike the viewDidLoad method, the viewWillAppear and viewDidAppear are called every time the ViewController is transitioned to. This makes the viewWillAppear a great place to setup the initial starting positions of animations and the viewDidAppear the perfect place to execute the animation.
Use Case 1: Simulate Screen Loading
Step 1: How We'll Delay
To simulate the loading delay we'll call DispatchQueue.main.asyncAfter(deadline:execute:) directly from the view controller — no helper file required. See Calling a Method After Delay for more background.
Step 2: Add/Configure Activity Indicator
It's super easy to take any ViewController you already have setup and incorporate simulated loading.
-
In the case that you have a ScrollView with an "Feed" ImageView, nest the Activity Indicator inside the ScrollView alongside the "Feed" ImageView. UIActivityIndicatorView, Nest the Child View

-
Make sure you select, Hides When Stopped in The Activity Indicator Attributes Inspector.

Step 3: Setup viewWillAppear
Just before the ViewController "appears", hide the "Feed" ImageView, and start animating the Activity Indicator.
// Right before the ViewController "appears"...
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// hide feed ImageView
feedImageView.isHidden = true
// turn on the activity indicator
loadingIndicator.startAnimating()
}
Step 4: Setup viewDidAppear
When the ViewController finally does "appear", delay for 2 seconds before stopping the Activity Indicator animation and showing the "Feed" ImageView.
// The moment the ViewController "appears"...
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Delay for 2 seconds before...
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
// show the feed ImageView
self.feedImageView.isHidden = false
// Stop the activity indicator
self.loadingIndicator.stopAnimating()
}
}
NOTE the variables within the asyncAfter closure require self., but don't worry — if you forget, Xcode will let you know and offer to fix the issue :)