How to display activity indicator in middle of the iphone screen?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A centered loading indicator is a small UI detail, but it often exposes bigger quality problems in iOS apps such as thread misuse, inconsistent cleanup, and blocked interaction. The real job is not just putting a spinner in the middle of the screen. It is managing loading state safely across success, failure, and cancellation paths.
Center a Spinner with Auto Layout in UIKit
For UIKit screens, use UIActivityIndicatorView with constraints. Avoid manual frame calculations because they drift on rotation and dynamic layout changes.
This keeps the spinner centered across device sizes, rotation changes, and later layout adjustments.
Add a Full Screen Overlay for Blocking Operations
If users should not interact while loading, place the spinner inside a dimmed overlay view. This communicates that the screen is temporarily busy.
An overlay is useful for short operations. For long operations, combine with progress text or a cancel action.
Keep Loading State Main Thread Safe
UI changes must be on the main actor. Also ensure hiding logic runs even if network calls fail.
defer is important here because it protects cleanup in all control paths.
SwiftUI Version with ProgressView
In SwiftUI, use a layered layout where loading state controls overlay visibility.
This approach is concise and works well with async tasks and state driven UI updates.
Accessibility and UX Considerations
Loading indicators should be informative, not decorative. In critical flows:
- Announce loading changes for assistive technologies.
- Prevent infinite spinners by enforcing timeouts.
- Offer retry if a request fails.
- Preserve user context after loading completes.
A spinner alone does not explain failure, so pair it with error messaging where needed.
Common Pitfalls
- Starting animation and forgetting to stop it on failed requests.
- Updating spinner state from background threads.
- Creating multiple overlapping spinners from repeated taps.
- Using a blocking overlay for long tasks with no status text.
- Hiding the spinner too early before dependent UI data is ready.
Summary
- Center indicators with Auto Layout in UIKit and state driven overlays in SwiftUI.
- Use a full screen overlay when interaction must be paused.
- Keep UI updates on the main actor and use
deferfor reliable cleanup. - Add accessible loading feedback for critical user journeys.
- Treat loading UI as part of error handling and screen state design, not a standalone widget.
Related reading
- How to display count of notifications in app launcher icon
- How to Display first 9 images in UICollectionview?
- How to display HTML in TextView?
- How to display HTML in TextView?
- How to display .svg image using swift
- How to display the default iOS 6 share action sheet with available share options?
- How to display Toast in Android?
- How to do a native Pulse effect animation on a UIButton - iOS
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.