Extending UICollectionView with a Centered UIActivityIndicatorView

How to add centered UIActivityIndicator in UICollectionView using Autolayout

Mohannad Bakbouk
2 min readNov 21, 2020

When you use UICollectionView you have to put a lot of UI views on storyboard to notify the user about the current status, for instance, we might put UIActivityIndicatorView while fetching data from API Services or another common example when there are no results that are matched the search filters.

So that would create a kind of repetition when our app has many UIActivityIndicatorView views, as a result, we are going to introduce an extension for UICollectionView to call it whenever we want that without adding any extra view into our storyboard as shown below in document outline.

First, create a method to show UIActivityIndicator

Now to use it just create an IBOutlet then we should call the method collectrionView.toggleActivityIndicator() on viewDidLoadAnd as soon as API request have finished we should recall it to hide it

Second, create another method to show a message with an icon to use it when there is an error or there are no fetched data

Now we can call our new method whenever there are no data or there is an error to inform the user the API request has finished but there is no response to display

So just call it

--

--